Firebase Realtime database getvalue() not mapping the object

后端 未结 4 800
长发绾君心
长发绾君心 2021-01-25 00:41

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:06

    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

    0 讨论(0)
  • 2021-01-25 01:07

    check the answer for this post this may help: I want to list all unique childrens and keys along with values in a spinner firebase

    In this, you can see how to fetch each of the node values then key along with their values.

     mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
    
            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
    
             //"It will give you all node values:"+dataSnapshot.getKey()
            //"You can check ": 
    
              if(dataSnapshot.getKey()=="amitpal"){
                for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()) {
    
             // "CHILD KEY"+childDataSnapshot2.getKey()
    
                    arrayList.add(childDataSnapshot2.getKey());
    
             //"CHILD VALUES"+childDataSnapshot2.getValue()
    
                }}
    
                }
    
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
    
        }
    });
    
    0 讨论(0)
  • 2021-01-25 01:10

    change your below line

    String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
    

    to

    String chatis=((DataSnapshot)iterator.next()).getValue().toString();
    

    Updated method below

        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    Log.e( "OUTER ADDED IS-" , dataSnapshot.getValue().toString());
           Iterator iterator=dataSnapshot.getChildren().iterator();
            while (iterator.hasNext()){
            DataSnapshot local = (DataSnapshot) iterator.next();
                String chatis=local.getValue().toString();
                Log.e( "Inner ADDED IS-" , chatis);
            }
    
        }
    

    EDIT

    also below line as well after the reference initialization

    dbreference.keepSynced(true);
    
    0 讨论(0)
  • 2021-01-25 01:32
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("ChatMessage" ).child( "msg").child("amitpal_name");
        ref.addValueEventListener(new ValueEventListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onDataChange ( @NonNull final DataSnapshot dataSnapshot2){
        }
    }
    
    0 讨论(0)
提交回复
热议问题