简体   繁体   中英

How to show items from two children in one RecyclerView from Firebase

Below is image representation of my firebase where user upload items under Electronics or Books category and also more categories. I want to build an activity with RecyclerView where I want to show the only item uploaded by a user. Since I'm using userid which is unique to push the details of the item inside a child, I can use userid to retrieve the items from child and display in firebase. But, how to search in each child from different category and show it in one RecyclerView .

在此处输入图片说明

My Code to Show the items from Electronics child is

dbreference = FirebaseDatabase.getInstance().getReference("Electronic").child("userid");

dbreference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      if (snapshot.exists()) {
         for (DataSnapshot data : snapshot.getChildren()) {

            final Solditem b1 = data.getValue(SoldItem.class);

            child_count++;
            list.add(b1);
            staggeredBooksAdapter.notifyDataSetChanged();
      }
    }
}

But I also would like to show the uploaded item by a user which equals to userid as key from Books in the same RecyclerView .

To delete an item from any category I use

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query sellerquery = ref.child("Electronic").child("userid");

sellerquery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
            appleSnapshot.getRef().removeValue();
        }
    }
}

To delete all the records of a user-uploaded item which key-value equals to userid , I need to repeat above code.

Is there any best and short method to delete all the records from any category which key-value equals to userid .

Thanks in advance.

While structuring your data in Firebase, you really need to think about the structure of your data very carefully so that you can run efficient queries on them. For example, in your case, you should have structured your data as follows.

userid
    - Electronics
        - Camera
        - Cooker
    - Books
        - A brief history of time
        - Inferno

Hence you could run your queries in such a way that, you could have all the items under a certain userid at once.

If you really need the structure that you have now for other sets of queries that you are planning to do in your application, think about replicating your data as well. That is, you might consider having duplicate entries of your data (replication) in different structure so that you can perform efficient queries. For example, the current structure of your data is suitable for the following query.

Get all userid under a specific category.

I think you have got my point. If you do not want to change the structure of your data then I am afraid that you might have to get all the data in your application from firebase first. Then you have to loop through the data yourself to find the elements that you actually needed.

Hope that helps!

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