I am unable to fetch the data from Firestore to Android. I am using the POJO (Java object class) method to get the values. How to proceed for this?
Pojo class:
public class Documents {
String documentID;
String documentName;
String documentDate;
String inspectorName;
String marketLocation;
public Documents() {
}
public Documents(String documentID, String documentName, String documentDate, String inspectorName, String marketLocation) {
this.documentID = documentID;
this.documentName = documentName;
this.documentDate = documentDate;
this.inspectorName = inspectorName;
this.marketLocation = marketLocation;
}
public String getDocumentID() {
return documentID;
}
public String getDocumentName() {
return documentName;
}
public String getDocumentDate() {
return documentDate;
}
public String getInspectorName() {
return inspectorName;
}
public String getMarketLocation() {
return marketLocation;
}
}
MainActivity.java:
List<Documents> documentsList = new ArrayList<>();
Documents documents = documentsList.get(); // Error to retrieve all data
primaryLayout(documents.getDocumentID(), documents.getDocumentName(), documents.getDocumentDate(), documents.getInspectorName(), documents.getMarketLocation());
This is a convention mistake as much as it is a blunder. You should name your POJO classes singular. That is, instead of naming it Documents
you should have named it Document
so that List<Document>
is documentsList
and Document document = documentsList.get(<index>)
makes sense.
Line Documents documents = documentsList.get();
is wrong since you are trying to get one Document
from the documentsList
by calling get()
method but you are not passing an index value to the get()
method. Also the documentsList
variable is initialized just above this line which means, the list is most probably empty.
What I suggest you do is:
Documents
to Document
.documentsList.add(new Document("documentID","documentName","documentDate","inspectorName","marketLocation"));
as many times as you have the Document
s from your firebase by using an iterator like foreach()
you have only initialized the ArrayList....Not it's objects....You have to initialize each of it's object and then you can access it by calling documentList.get(position).. However this will not solve your problem of getting data from Firebase FireStore.. You can follow this link adding fireStore to android
More code from MainActivity class
MainActivity.java:
List<Documents> documentsList = new ArrayList<>();
Documents documents = documentsList.get(); // Error to retrieve all data
primaryLayout(documents.getDocumentID(), documents.getDocumentName(), documents.getDocumentDate(), documents.getInspectorName(), documents.getMarketLocation());
public void primaryLayout(String documentID, String documentName, String documentDate, String inspectorName, String marketLocation) {
progressBar = findViewById(R.id.progressLoader);
progressBar.setVisibility(View.VISIBLE);
Documents documents = new Documents(documentID, documentName, documentDate, inspectorName, marketLocation);
collectionReference.document(documents.getDocumentID()).collection("Products").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
itemsList.clear();
for(DocumentSnapshot documentSnapshot : task.getResult()) {
Items items = documentSnapshot.toObject(Items.class);
itemsList.add(items);
productListAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
}
}
});
}
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.