簡體   English   中英

我想在 firebase 數據庫中一一獲取所有子項的子項

[英]I want to get all child of child items one by one in firebase database

這是 model class:

我希望我一一獲取所有類別的所有食品,而不是在回收站視圖中顯示每一個食品數據

在獲得我想要的所有項目的列表后,我只在列表中添加那些 pin 值為 TRUE 的項目

public class Items {
    String name, desc,image, category;
    int price;
    boolean pin;


    public Items(String name, String desc, String image, String category, int price, boolean pin) {
        this.name = name;
        this.desc = desc;
        this.image = image;
        this.category = category;
        this.price = price;
        this.pin = pin;
    }

    public Items() {
    }

    public boolean isPin() {
        return pin;
    }

    public void setPin(boolean pin) {
        this.pin = pin;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

這是 java class 文件代碼:當我在日志中使用 snapshot.getValue() 檢查快照時,它為我提供了所有類別和項目,但它沒有添加到列表中......為什么???

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("admin");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list.getClass();
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    Items items = dataSnapshot.getValue(Items.class);
                    list.add(items);
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

這是我的數據庫:

我想要所有類別的所有食品一一

for (DataSnapshot dataSnapshot: snapshot.getChildren()){循環僅在數據庫中的一個級別上循環,因此您的dataSnapshot指向BBQ然后指向Chines ,而不是單個食品。

由於您想遍歷兩個嵌套節點級別,因此您的代碼中需要兩個嵌套循環:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("admin");
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        list.getClass();
        for (DataSnapshot categorySnapshot : snapshot.getChildren()){ // 👈
            for (DataSnapshot itemSnapshot : categorySnapshot.getChildren()){ // 👈
                Items items = itemSnapshot.getValue(Items.class); // 👈
                list.add(items);
            }
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // 👈 Never ignore errors
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM