[英]How to retrieve list from POJO class
I am unable to fetch the data from Firestore to Android.我无法从 Firestore 获取数据到 Android。 I am using the POJO (Java object class) method to get the values.我正在使用 POJO(Java object 类)方法来获取值。 How to proceed for this?如何进行此操作?
Pojo class:宝卓 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: 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.您应该将 POJO 类命名为单数。 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.也就是说,与其将其命名为Documents
,不如将其命名为Document
,以便List<Document>
是documentsList
并且Document document = documentsList.get(<index>)
是有意义的。
Line Documents documents = documentsList.get();
行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.是错误的,因为您试图通过调用get()
方法从documentsList
列表中获取一个Document
,但您没有将索引值传递给get()
方法。 Also the documentsList
variable is initialized just above this line which means, the list is most probably empty.此外, documentsList
变量在这一行上方初始化,这意味着列表很可能是空的。
What I suggest you do is:我建议你做的是:
Documents
to Document
.首先 Refactor> 将 POJO class 从Documents
重命名为Document
。documentsList.add(new Document("documentID","documentName","documentDate","inspectorName","marketLocation"));
其次,在初始化文档列表后,通过调用将 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()
使用像foreach()
这样的迭代器从 firebase 获得Document
的次数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您只初始化了 ArrayList....不是对象....您必须初始化每个 object 然后您可以通过调用 documentList.get(position).. 来访问它。但是这不会解决您获取的问题来自 Firebase FireStore 的数据。您可以点击此链接将 fireStore 添加到 android
More code from MainActivity class更多代码来自 MainActivity class
MainActivity.java: 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);
}
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.