emowise
emowise

Reputation: 141

Android firebase nested-listeners data change problem

I'm working with Firebase realtime database. I have a friend list for each user. I'm adding friend list of user to recyclerview. There is no error I can see friend list. But if I change something about user friend information, there is no change in the adapter. If I add a new user to the friend list, adapter senses the changes. I use nested firebase listener. First listener for getting user friend ids, second listener for fetching friend information using ids.

I want if the user friend changes its online status, adapter senses it.

This is my json tree:  json tree

This is my code for fetching user friend list:

        databaseReference.child("users").child(user_id).child("friends").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //friends.clear();
                //friendInfoList.clear();
            //Toast.makeText(FriendsActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
                for(DataSnapshot ds:dataSnapshot.getChildren()){
                    String friend_id=ds.getValue(String.class);
                    friends.add(new Friends(friend_id));
                    Toast.makeText(FriendsActivity.this, friend_id, Toast.LENGTH_SHORT).show();

                    databaseReference.child("users").child(friend_id).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            //friendInfoList.clear();
                                Users user=dataSnapshot.getValue(Users.class);
                                friendInfoList.add(new FriendInfo(user.getUsername(),user.getCountry(),user.isStatus()));
                        }

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

                        }
                    });

                }

           adapter=new FriendListRecyclerAdapter(FriendsActivity.this,friendInfoList);
            recyclerView.setAdapter(adapter);


        }

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

        }
    });

Upvotes: 0

Views: 703

Answers (2)

furkanbzkurt
furkanbzkurt

Reputation: 356

Probably, you are not triggering "users/user_id/friends" node when friend information has been changed. Also I think you should use "child event listener".

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80914

To update the data in the adapter call the method notifyDataSetChanged().

From the docs:

notifyDataSetChanged

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

Upvotes: 1

Related Questions