如何从Firebase检索对象列表

时间:2016-12-18 11:50:29

标签: android firebase firebase-realtime-database

我有一个users列表,每个列表都有一个名为chatRoomList的对象列表。enter image description here

如何从我的数据库中检索此列表? 不幸的是我不能这样做:

 User user = dataSnapshot.getValue(User.class);
                if (user.getObjectId().equals(myId)) {
                List<ActiveChatRoom> list = user.getActiveChatRooms();

                }

我试图遍历结果

 for (DataSnapshot child : dataSnapshot.getChildren()) {

                        Log.e(TAG, "User val " + child.getValue().toString());

                    }

但我不知道如何处理我得到的价值:

User val {-KZGY1PWEFgMoHIap8Ew={receiversName=Test2, chatRoomId=-KZGY1KxX6zeUr3zAVNL, receiversPic=www.url.com}, -KZGXztjZ73GhCaabrBg={receiversName=Test, chatRoomId=-KZGXzp5jp4br0AL6edD, receiversPic=www.url.com}}

2 个答案:

答案 0 :(得分:1)

User类需要包含以下内容(例如,假设您有ChatRoom java类:

Map<String,ChatRoom> chatRoomList;

答案 1 :(得分:0)

DatabaseReference frndMuteRef = FirebaseDatabase.getInstance().getReference().child("users/" + getFirebaseUser().getUid().trim() + "/friendlists");
        Query queryRef = frndMuteRef.orderByChild(MUTE).equalTo(YES);
        queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                GenericTypeIndicator<HashMap<String,MuteModel>> t=  new GenericTypeIndicator<HashMap<String,MuteModel>>() { };

                HashMap<String,MuteModel> hashMap= (HashMap<String,MuteModel>)dataSnapshot.getValue(t);

                if(hashMap!=null) {
                   muteModelList = new ArrayList<MuteModel>(hashMap.values());
                    for (MuteModel muteModel: muteModelList){
                        muteModel.setIsgroup(NO);
                    }

                }else{
                   muteModelList = new ArrayList<MuteModel>();


                }



            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

包含MuteModel只是传递你的聊天模型对象。使用像yourrootreference +"/chatRoomList"这样的引用。我认为这将完成工作