Reputation: 57
I am trying to read data that I inserted into firebase database, I want to display the data in custom listView. I am using this method but it seems that I am missing something.
I can't insert data into my arraylist.
UPDATED I inserted the values and now it gives me
attempt to invoke a virtual method on a null object reference on my get methods
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
PatientInformation patientInfo = new PatientInformation();
patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
ArrayList<PatientInformation> arrayList = new ArrayList<>();
arrayList.add(patientInfo);
PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
listView.setAdapter(pAdapter);
}
Upvotes: 1
Views: 299
Reputation: 600130
You're creating a new array list and adapter for every child node of the results. You probably want to create only one array list and adapter, like this:
private void showData(DataSnapshot dataSnapshot) {
ArrayList<PatientInformation> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
PatientInformation patientInfo = ds.child(userID).getValue(PatientInformation.class)
arrayList.add(patientInfo);
}
PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
listView.setAdapter(pAdapter);
}
Upvotes: 1
Reputation: 1223
Your arrayList is of type PatientInformation so I think you could just add patientInfo which is of type PatientInformation also.
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
PatientInformation patientInfo = new PatientInformation();
patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
ArrayList<PatientInformation> arrayList = new ArrayList<>();
arrayList.add(patientInfo);
PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
listView.setAdapter(pAdapter);
}
Upvotes: 1