我无法从Firestore提取数据到Android。我正在使用POJO(Java对象类)方法来获取值。如何进行呢?
Pojo类:
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());
答案 0 :(得分:2)
这既是惯例错误,也有失误。您应该将POJO类命名为单数。也就是说,应该命名为Documents
,而不是命名Document
,这样List<Document>
就是documentsList
,而Document document = documentsList.get(<index>)
才有意义。
第Documents documents = documentsList.get();
行是错误的,因为您试图通过调用Document
方法从documentsList
中获得一个get()
,但没有将索引值传递给{{ 1}}方法。另外,get()
变量在此行的上方进行了初始化,这意味着该列表很可能为空。
我建议您做的是:
documentsList
重命名为Documents
。 Document
到您的Firebase中获得documentsList.add(new Document("documentID","documentName","documentDate","inspectorName","marketLocation"));
的次数,使用{ {1}} 答案 1 :(得分:0)
您只初始化了ArrayList...。不是它的对象。...您必须初始化它的每个对象,然后可以通过调用documentList.get(position)来访问它。 但是,这不能解决您从Firebase FireStore获取数据的问题。 您可以点击此链接adding fireStore to android
答案 2 :(得分:0)
MainActivity类的更多代码
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);
}
}
}
});
}