Firebase Realtime database getvalue() not mapping the object

拟墨画扇 提交于 2019-12-31 04:30:12

问题


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

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

    dbreference.orderByChild("Ref").addChildEventListener( new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {[![enter image description here][1]][1]
           Iterator iterator=dataSnapshot.getChildren().iterator();
            while (iterator.hasNext()){
                String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
                Log.e( "ADDED IS-" , chatis);
            }

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            Iterator iterator=dataSnapshot.getChildren().iterator();
            while (iterator.hasNext()){
                String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
                Log.e( "ADDED chng-" , chatis);
            }
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.e( "ERRO FBDB-", databaseError.getMessage());

        }
    } );

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

but it not providing any info or values pls help me here

I have also try this below method giving me good and expected result but the main problem is i want to reflect the new value in listview when databse update in firebaserealtime Chat without using service, for that i need this method below

public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s)


回答1:


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




回答2:


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) {

    }
});



回答3:


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);


来源:https://stackoverflow.com/questions/55016979/firebase-realtime-database-getvalue-not-mapping-the-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!