在Android中获取数据deom Firestore

时间:2018-05-07 05:15:36

标签: android google-cloud-firestore

我在Firestore中有如下数据,我想获取该数据。我在 QueryDocumentSnapshot 对象列表中获取数据。我想要ArrayList中的数据。

{orderedItemsList:[
0={itemDescription:"These popular street dumplings are so versatile. You just need to master the technique and you can twist them to suit your palate.",
itemId:null,
itemImageURL:"https://www.ndtv.com/cooks/images/momos%20%281%29.jpg",
itemPrice:"40",
itemQuantity:12,
itemStatus:0,
itemTitle:"Veg Momos"
},
1:{itemDescription:"Who doesn't love burgers? Aloo Tikki Burger is loved by people of all age groups. This Fusion appetizer is made using simple ingredients like veg mayonnaise, burger bun, fried aloo tikki, sliced onions and tomatoes.".
itemId:null,
itemImageURL:"https://recipes.timesofindia.com/photo/62068349.cms?imgsize=342277",
itemPrice:"79",
itemQuantity:5,
itemStatus:0,
itemTitle:"Aloo Tikki Burger"}],
userEmail:"roshannimje94@gmail.com"}

1 个答案:

答案 0 :(得分:0)

试试这个

    private List<Parties> newsList = new ArrayList<>();


   db.collection("dbref").orderBy("name", Query.Direction.ASCENDING).addSnapshotListener(this,new EventListener<QuerySnapshot>() {
             @Override
             public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                 for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
                     String id = doc.getDocument().getId();
                     NewsFeed news = doc.getDocument().toObject(NewsFeed.class).withId(id);
                     switch (doc.getType()) {
                         case ADDED:
                             newsList.add(news);
                             mAdapter.notifyDataSetChanged();
                             break;
                         case MODIFIED:
                             newsList.remove(news);
                             newsList.add(news);
                             mAdapter.notifyDataSetChanged();
                             break;
                         case REMOVED:
                             newsList.remove(news);
                             mAdapter.notifyDataSetChanged();
                             break;
                     }
                 }

             }
         });

Pojo将是

public class NewsFeed {


    String userEmail,itemTitle,itemDescription,itemId,itemPrice,itemImageURL,
            itemQuantity,itemStatus;

    public NewsFeed(String userEmail, String itemTitle, String itemDescription, String itemId, String itemPrice, String itemImageURL) {
        this.userEmail = userEmail;
        this.itemTitle = itemTitle;
        this.itemDescription = itemDescription;
        this.itemId = itemId;
        this.itemPrice = itemPrice;
        this.itemImageURL = itemImageURL;
    }
    public NewsFeed() {
    }

    public String getUserEmail() {
        return userEmail;
    }


    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getItemTitle() {
        return itemTitle;
    }

    public void setItemTitle(String itemTitle) {
        this.itemTitle = itemTitle;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public String getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(String itemPrice) {
        this.itemPrice = itemPrice;
    }

    public String getItemImageURL() {
        return itemImageURL;
    }

    public void setItemImageURL(String itemImageURL) {
        this.itemImageURL = itemImageURL;
    }

    public String getItemQuantity() {
        return itemQuantity;
    }

    public void setItemQuantity(String itemQuantity) {
        this.itemQuantity = itemQuantity;
    }

    public String getItemStatus() {
        return itemStatus;
    }

    public void setItemStatus(String itemStatus) {
        this.itemStatus = itemStatus;
    }



}