-1

I have two tables in firebase database.

Table A :       Table B:

a               a  2 pic
b               b  3 pic
c
d               d  1 pic
e

Result shows as :

a  2 pic
b  3 pic
d  1 pic

I need result as :

a  2 pic
b  3 pic
c  0 pic
d  1 pic
e  0 pic

I am unable How I will write the query.

I write the query as if a from Table A, equals a in Table B, then a will show with 2 pic. In this way, a,b and d shows. But c and e are not showing as c and e not equal in Table B. I want to show c and e with 0 pictures.

What I have tried:

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list = new ArrayList<ListType>();
            list.clear();
            for(DataSnapshot ds:dataSnapshot.getChildren()) {
                ListType listType = ds.getValue(ListType.class);
                final String listname = listType.getName();
                
                databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
                           if (postSnapshot.getKey().equals(listname)) {
                                String count = String.valueOf(postSnapshot.getChildrenCount());
                                ListType listTypee = new ListType(listname, count);
                                list.add(listTypee);
                                adapter.notifyDataSetChanged();
                           }
                        }
                    }

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

                    }
                });
            }
achal naskar
  • 710
  • 1
  • 6
  • 19

1 Answers1

1

You're now looping over all nodes in table B, and adding their counts to the list. But since C and E are not in table B, they'll never show up in the list.

I'd actually invert the logic, and:

  1. Loop over the results of reading table A
  2. Check if there are nodes for that in table B
  3. Add either the number of nodes or 0

In code that'd be something like:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list = new ArrayList<ListType>();
        list.clear();
        for(DataSnapshot ds: dataSnapshot.getChildren()) {
            ListType listType = ds.getValue(ListType.class);
            final String listname = listType.getName();
            
            databaseReference1.child(listname).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot bSnapshot) {
                    String count = String.valueOf(bSnapshot.getChildrenCount());
                    ListType listTypee = new ListType(listname, count);
                    list.add(listTypee);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException(); // never ignore errors
                }
            });
        }

Note that the individual listeners inside the loop are not nearly as slow as you may think, since Firebase pipelines the requests.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807