简体   繁体   中英

How to find firebase all children details from different users?

I want to show all product child details. but I was unable to do so. I am new to firebase and wish to complete my university project I need to use firebase. 在此处输入图像描述

在此处输入图像描述

each user has a single or multiple products. I can show all shops from a single user but I want to show all products from all user.

    private void loadShopProducts() {
        productList = new ArrayList<>();

       DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
       ref.child(shopUid).child("Products")
               .addValueEventListener(new ValueEventListener() {
                   @Override
                   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                       productList.clear();
                       for (DataSnapshot ds : dataSnapshot.getChildren()){
                           ModelProduct modelProduct = ds.getValue(ModelProduct.class);
                           productList.add(modelProduct);
                    }
                      adapterProductBuyer = new 
                      AdapterProductBuyer(ShopDetailsActivity.this, productList);
                    productRv.setAdapter(adapterProductBuyer);
                }

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

                }
            });
        }

Basically, I need to pull out all the children from users who are the seller.

private void loadAllProduct(){
            productList = new ArrayList<>();
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    productList.clear();
                    for (DataSnapshot ds : dataSnapshot.getChildren()){
                        String uid = ""+ds.getRef().getKey();
    
                        DatabaseReference ref = (DatabaseReference) FirebaseDatabase.getInstance().getReference("Users").child(uid).child("Products");
                             //   ref.addValueEventListener(new ValueEventListener() {
                                ref.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                        productList.clear();
                                        if (dataSnapshot.exists()){
                                            for (DataSnapshot ds : dataSnapshot.getChildren()){
                                                ModelProduct modelProduct = ds.getValue(ModelProduct.class);
    
                                                productList.add(modelProduct);
                                            }
    
                                            adapterProductShow = new AdapterProductShow(MainBuyerActivity.this, productList);
                                            showAllProductRv.setAdapter(adapterProductShow);
                                        }
                                    }
    
                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {
    
                                    }
                                });
    
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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