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.
And all I get is just this list of empty items.
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