Firebase Realtime database getvalue() not mapping the object

后端 未结 4 1054
耶瑟儿~
耶瑟儿~ 2021-01-25 00:46

This is my firebase chat structure(see image below). I want to get only amitpal_name value not the others but this method fetching me all the values
I had also use this

4条回答
  •  不思量自难忘°
    2021-01-25 01:14

    here is your reference

        refAmitPat = FirebaseDatabase.getInstance().getReference().child("ChatMessage").child("msg");
    

    and here is child event listner on your DB Reference

    refAmitPat.child("amitpal_name").addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    Log.d("amitPal",dataSnapshot.getValue()+"");
                    // add message value to list and call notify datasetchange method 
                }
    
                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    Log.d("amitPal",dataSnapshot.getValue()+"");
                }
    
                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
    
                }
    
                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
    

    what are you doing, wrong is, you are iterating the datasnapshot whereas when you push the message under usernames as child, the child will get as the datasnapshot in childAdded method, means the data snapshot your are iterating, is actually your message with push-key and message value. so there's no need to iterate through that. simply call datasnapshot.getValue() function to get message value

提交回复
热议问题