简体   繁体   English

如何从 POJO class 中检索列表

[英]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?如何进行此操作?

1

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:我建议你做的是:

  1. First off Refactor> Rename your POJO class from Documents to Document .首先 Refactor> 将 POJO class 从Documents重命名为Document
  2. Second, after initializing the list of documents, add the instances of Document to the list by calling: 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.

相关问题 如何通过Generic Class从MongoDB中检索POJO - How to retrieve a POJO from MongoDB via a Generic Class 如何从json rest请求中检索Arraylist / list并转换为pojo对象(pojo变量之一是列表类型。) - How to retrieve Arraylist/list from json rest request and convert into pojo object (one of pojo variable is of list type.) 如何使用Pojo从数据存储中检索实体? - How to retrieve entities from datastore by using pojo? 如何过滤pojo类并从中创建新的pojo类 - How to filter pojo class and make new pojo class from them 如何从 class 中检索已弃用的方法列表 - How to retrieve a list of deprecated methods from a class solrj:如何存储和检索列表<pojo>通过索引中的多值字段</pojo> - solrj: how to store and retrieve List<POJO> via multivalued field in index 如何从xml生成pojo类(在jvm中)? - How to generate a pojo class ( in jvm) from a xml? 如何从Pojo列表中获取值 - How to get values from pojo list 将属性从pojo类列表中移到该属性的数组中 - Take an attribute from a list of pojo class to an array of that attribute 我无法通过pojo类对象从firebase检索嵌套的子对象 - I cannot retrieve nested child from firebase through pojo class object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM