0

I want to get sender_uid values on the image below:

enter image description here

This is my code below. I don't get any values with it, and I don't know why. Can someone explain what is wrong with the code? Thanks!

EDIT: I only want the last sender_uid for each parent - for example:

  • for node 391rfBEXjVUplEW8aoahMtluSmz1_6dTv7oHnWOh55XzRuf9ouZFZkGt2
    I need only sender_uid from LJxvDjdetWz1-DAFVCy, NOT from LJxvA65WA7MX8aaX5nF.

    mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                List<UsersListGetter> usersList = new ArrayList<>();
                recyclerView.setAdapter(new UsersListAdapter(usersList, getApplicationContext()));
    
                recyclerView.setAdapter(new UsersListAdapter(null, getApplicationContext()));
    
                for (DataSnapshot ch : dataSnapshot.getChildren()) {
                    String senderReceiver[] = ch.getKey().split("_");
    
                    if (senderReceiver[0].equals(mAuth.getUid()) || senderReceiver[1].equals(mAuth.getUid())) {
                        Query lastQuery = mDatabase.child(ch.getKey()).orderByKey().limitToLast(1);
                        lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String message = dataSnapshot.child("message_body").getValue().toString();
                                Log.d("MSG", message);
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                //Handle possible errors.
                            }
                        });
                    }
                }
            }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
    
        }
    });
    
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
unipin
  • 259
  • 1
  • 3
  • 13
  • Possible duplicate of [i need to get Last child of my firebase databse](https://stackoverflow.com/questions/43557122/i-need-to-get-last-child-of-my-firebase-databse) – Usman Ali Aug 16 '18 at 13:08

2 Answers2

1

You can do it this way to get the sender_uid from each node

First create a pojo class to get your desired data

public class UserPojo {

    private String sender_uid;

    public UserPojo() {

    }

    public String getSender_uid() {
        return sender_uid;
    }

    public void setSender_uid(String sender_uid) {
        this.sender_uid = sender_uid;
    }
}

and then just retrieve it for each chat ref

mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                List<UsersListGetter> usersList = new ArrayList<>();
                recyclerView.setAdapter(new UsersListAdapter(usersList, getApplicationContext()));

                recyclerView.setAdapter(new UsersListAdapter(null, getApplicationContext()));

                for (DataSnapshot ch : dataSnapshot.getChildren()) {
                  UserPojo up = ch.getValue(UserPojo.class);
                  String senderUid = up.getSender_uid(); //here you got your sender_uid

                        Query lastQuery = mDatabase.child(ch.getKey()).orderByKey().limitToLast(1);
                        lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String message = dataSnapshot.child("message_body").getValue().toString();
                                Log.d("MSG", message);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                //Handle possible errors.
                            }
                        });

                }
            }

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

        }
    });
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

Assuming that the chat node is a direct child of your Firebase root, to solve this, please use the following query:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef
    .child("chat")
    .child(receiver_id + "_" + sender_uid)
    .orderByChild("sender_uid")
    .limitToLast(1);
query.addListenerForSingleValueEvent(/* ... */);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193