Kumar
Kumar

Reputation: 979

Run a Firebase Realtime Database query multiple times using for loop in android not working

I am trying to run a Firebase Realtime database query in for loop its not working. also not able to view data in RecyclerView. kindly help me fix this issue. thanks in advance.

privte void mLoadData()
{

    for(User user:nameArraylist)
            {
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
                Query query = databaseReference.child("AllUsers").child(user.getId()).limitToLast(1);
                query.addValueEventListener(new ValueEventListener()
                {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for (DataSnapshot ds : snapshot.getChildren())
                        {
                            try {
                                USerPojo userPojo = new USerPojo ();
                                
                                userPojo.setType(ds.child("type").getValue().toString());
                                
    
                                userArrayList.add(userPojo);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                        }
                    }
    
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
    
                    }
    
    
                });
    
            }

 
                sAdapterNew = new AdapterNew(getActivity(), userArrayList);
                groupRecyclerView.setAdapter(sAdapterNew );
                sAdapterNew .notifyDataSetChanged();
   
}

Upvotes: 0

Views: 572

Answers (2)

androidLearner
androidLearner

Reputation: 1702

In firebase ,all callback methods are Async method.That's why adapter gets called before try block.So if recycler view needs data from onDataChange() to populates it's view,we need to move adapter code within onDataChange().

        privte void mLoadData(List<user> nameArraylist)
        {
         int i;
         for(i=0;i<nameArraylist.size();i++)
            
                    {
                        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
                        Query query = databaseReference.child("AllUsers").child(nameArraylist.get(i).getId()).limitToLast(1);
                        query.addValueEventListener(new ValueEventListener()
                        {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                for (DataSnapshot ds : snapshot.getChildren())
                                {
                                    try {
                                        USerPojo userPojo = new USerPojo ();
                                        
                                        userPojo.setType(ds.child("type").getValue().toString());
                                        
            
                                        userArrayList.add(userPojo);

    //when you run outer for() loop to get data,every time adapter will set new value to recycler view  till loop reach last value.
   //so to avoid that i added if() condition,so that adapter will set all value at once.
     


           if(i==nameArraylist.size()-1) {
                                   sAdapterNew = new AdapterNew(getActivity(), userArrayList);
                                   groupRecyclerView.setAdapter(sAdapterNew );
                                   sAdapterNew .notifyDataSetChanged();
        
                                   }
        
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                
                                    }
                                }
                
                                @Override
                                public void onCancelled(@NonNull DatabaseError error) {
                
                                }
                
                
                            });
                
                        }
            
             
                        
               
            }

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

There are many possible problems with your code, which is why I left a comment that should help you to debug.

But one certain problem is that you're not calling sAdapterNew .notifyDataSetChanged() after adding items to userArrayList. The UI will not be updated unless you call notifyDataSetChanged, so you'll want to call that in onDataChange too.

Something like:

public void onDataChange(@NonNull DataSnapshot snapshot) {
    for (DataSnapshot ds : snapshot.getChildren()) {
        try {
            USerPojo userPojo = new USerPojo();
            userPojo.setType(ds.child("type").getValue().toString());
            userArrayList.add(userPojo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sAdapterNew.notifyDataSetChanged();
    }
}

Upvotes: 2

Related Questions