使用 Firebase 数据库获取每个孩子的最后一个孩子

我想获取下图的sender_uid值:

http://img.mukewang.com/60fa8ca20001e45a05910478.jpg

这是我下面的代码。我没有得到任何价值,我不知道为什么。有人可以解释代码有什么问题吗?谢谢!


编辑:我只想要每个父级的最后一个 sender_uid - 例如:


对于节点,391rfBEXjVUplEW8aoahMtluSmz1_6dTv7oHnWOh55XzRuf9ouZFZkGt2

我只需要来自LJxvDjdetWz1-DAFVCy 的 sender_uid,而不是来自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) {


    }

});


料青山看我应如是
浏览 192回答 2
2回答

陪伴而非守候

您可以通过这种方式从每个节点获取 sender_uid首先创建一个pojo类来获取你想要的数据public class UserPojo {&nbsp; &nbsp; private String sender_uid;&nbsp; &nbsp; public UserPojo() {&nbsp; &nbsp; }&nbsp; &nbsp; public String getSender_uid() {&nbsp; &nbsp; &nbsp; &nbsp; return sender_uid;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSender_uid(String sender_uid) {&nbsp; &nbsp; &nbsp; &nbsp; this.sender_uid = sender_uid;&nbsp; &nbsp; }}然后为每个聊天参考检索它mDatabase.addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<UsersListGetter> usersList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(new UsersListAdapter(usersList, getApplicationContext()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(new UsersListAdapter(null, getApplicationContext()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot ch : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserPojo up = ch.getValue(UserPojo.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String senderUid = up.getSender_uid(); //here you got your sender_uid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query lastQuery = mDatabase.child(ch.getKey()).orderByKey().limitToLast(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = dataSnapshot.child("message_body").getValue().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("MSG", message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Handle possible errors.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java