0

I'm having an issue trying to get some data from Firebase. I've done this before and never had a problem till now. I have a firebase helper that loads the data then adds it to a custom arraylist. Now, the data is showing but not been added to the arraylist and I'm not sure why.

  • Doing a bit of testing, I noticed the "ListData data = ds.getValue(ListData.class)" where the data is populated is not working. It's won't set the data properly to the ListData class. But I don't know why.

Here is the code to get the data from the database:

class FirebaseHelper {

// objects
private final DatabaseReference db;
private final ArrayList<ListData> contactData = new ArrayList<>();
private final HomeActivity activity;

// ------------------------------

FirebaseHelper(DatabaseReference db, HomeActivity ac){
    this.db = db;
    this.activity = ac;
}

// ------------------------------

private void fetchData(DataSnapshot dSnap){

    // Clear arraylist
    contactData.clear();

    // Populate with new data
    for(DataSnapshot ds: dSnap.getChildren() ) {

        ListData data = ds.getValue(ListData.class);
        contactData.add(data);

    }

    // TODO: Setup listview and adapter after data has loaded

    ContactAdapter adapter = new ContactAdapter(activity, contactData, activity);
    ListView listView = activity.findViewById(R.id.itemList);
    listView.setAdapter(adapter);

} // end fetchData

// ------------------------------

ArrayList<ListData> retrieve() {

    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    assert user != null;

    db.child("contacts").child(user.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            fetchData(dataSnapshot);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return contactData;

 } // end retrieve

} // end FirebaseHelper

Now, I get data, but it won't add.

enter image description here

And all I get is just this list of empty items.

enter image description here

If any more information is needed, please let me know.

Here's the ListData class

 class ListData implements Serializable {

private String contactName;
private String contactNum;
private int contactAge;
private String userId;

ListData(){}

// setter

public void setContactAge(int contactAge) {
    this.contactAge = contactAge;
}

public void setContactName(String contactName) {
    this.contactName = contactName;
}

public void setContactNum(String contactNum) {
    this.contactNum = contactNum;
}

public void setUserId(String userId) {
    this.userId = userId;
}

// getter

String getContactName() {
    return contactName;
}

String getContactNum() {
    return contactNum;
}

int getContactAge() {
    return contactAge;
}

String getUserId() {
    return userId;
 }

} // end ListData
Nyxx
  • 303
  • 3
  • 13
  • Can you please show us the `ListData.class`? – Joaquim Ley Mar 03 '18 at 22:18
  • Please post complete code for helper class – Levi Moreira Mar 03 '18 at 22:20
  • Just added all the information. – Nyxx Mar 03 '18 at 22:21
  • 3
    Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Mar 04 '18 at 10:58
  • @AlexMamo Actually, I'm getting all my data from the onDataChange. I was doing a few logs and I noticed this part wasn't working properly.. ListData data = ds.getValue(ListData.class); where the data is populated, but I'm not sure why it's not working. – Nyxx Mar 04 '18 at 15:45
  • @AlexMamo No, I figured it out tho. It was an error in my ListData class. I posted the answer. – Nyxx Mar 05 '18 at 14:30

1 Answers1

0

I figure it out. I wan't sure why the data wasn't posting even though it was there. I didn't know the objects (string, int) in ListData have to be named exactly as the key/values on the data been retrieved. Once I changed all the names on the ListData to the correct key values, all the data is displayed now.

Nyxx
  • 303
  • 3
  • 13