2 回答
TA贡献1757条经验 获得超8个赞
您可以通过这种方式从每个节点获取 sender_uid
首先创建一个pojo类来获取你想要的数据
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;
}
}
然后为每个聊天参考检索它
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) {
}
});
添加回答
举报