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);
}
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);
}
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.