简体   繁体   English

无法从Firebase获取RecyclerView的所有结果

[英]Not getting all results for RecyclerView from firebase

My app has two categories of users shop owner and buyers. 我的应用有两类用户:商店所有者和购买者。 When shop owner adds a shop I use his UID and then pushkey to add a shop like this: 当商店老板添加商店时,我使用他的UID,然后使用按键添加商店,如下所示:

这个

In the Image "83yV" and "FmNu" are the shop owners and they add 2 and 1 shop respectively. 在图像中,“ 83yV”和“ FmNu”是商店所有者,他们分别添加2和1个商店。

Now for buyers, I want to show the whole list of shops in a Recycler View. 现在,对于买家,我想在“回收者视图”中显示整个商店清单。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_shops_avtivity);

    shopsDB = FirebaseDatabase.getInstance().getReference().child("shops");
}

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Shop,ShopViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Shop, ShopViewHolder>(
            Shop.class,
            R.layout.shop_single_layout,
            ShopViewHolder.class,
            shopsDB)
}

The problem is when I point my Firebase Recycler Adapter to shopDB, it returns two empty cardView like this: 问题是,当我将Firebase Recycler Adapter指向shopDB时,它返回两个空的cardView,如下所示:

这个

When I point it to specific child like 当我把它指向特定的孩子时

shopsDB = FirebaseDatabase.getInstance().getReference().child("shops").child("83yV24a3AmP15XubhPGApvlU7GE2");

It returns shops add by "83yV". 返还商店加“ 83yV”。

How can I get the shops added by other owners also? 我如何也可以得到其他所有者添加的商店? Preferably without altering current DB or creating one DB with all shops in it within on child. 最好不要更改当前的DB或在子级中创建一个包含所有商店的DB。

To achieve this, you need to use getChildren() method twice, once to get all the users and second to get all the shops. 为此,您需要使用getChildren()方法两次,一次是获取所有用户,第二次是获取所有商店。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference shopsRef = rootRef.child("shops");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                String name = dSnapshot.child("name").getValue(String.class);
                Log.d("TAG", name);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
shopsRef.addListenerForSingleValueEvent(eventListener);

Using this code, you'll be able to display all shop names to the buyers. 使用此代码,您可以向买家显示所有商店名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM