Vishal Yadav
Vishal Yadav

Reputation: 1024

how get value from Firebase

I am new in android and I want to get userIDS from Firebase Database, I've tried by using this but it returns null.


    private void getMYuid() {
    String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference mTest = FirebaseDatabase.getInstance().getReference();
    mTest.child(Constants.ARG_CHAT_GROUP_ROOMS).child(Constants.NEW_NODE)
            .child(senderUid).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        if (!dataSnapshot.exists()){
            Toast.makeText(ActivityChatView.this, "not exist", Toast.LENGTH_SHORT).show();
            Log.e("151","ACV"+dataSnapshot);
        }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    Log.e("139","ACV"+senderUid);
    }


enter image description here

Upvotes: 0

Views: 143

Answers (2)

Laert
Laert

Reputation: 166

if you want the first one under 15052169227329_myGroupName_Hell By Anne: Your problem is that you forgot the node before "Constants.NEW_NODE"

private void getMYuid() {
        String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference mTest = FirebaseDatabase.getInstance().getReference();
        mTest.child(Constants.ARG_CHAT_GROUP_ROOMS).child("15052169227329_myGroupName_Hell By Anne").child(Constants.NEW_NODE)
                .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (!dataSnapshot.exists()){
                    Toast.makeText(ActivityChatView.this, "not exist", Toast.LENGTH_SHORT).show();
                    Log.e("151","ACV"+dataSnapshot);
                }

                // You can cast this object later but it seems that that is a string and not an array
                Object yourRequiredObject = dataSnapshot.child("usersIDS").getValue();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        Log.e("139","ACV"+senderUid);
    }

Upvotes: 3

Alex Mamo
Alex Mamo

Reputation: 138824

As i see, it's not an array it's a String. To get the userIDS, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child(Constants.ARG_CHAT_GROUP_ROOMS).child(senderUid).child(Constants.NEW_NODE);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String usersIDS = dataSnapshot.child("usersIDS").getValue(String.class);
        Log.d("TAG", usersIDS);
        //Here you can split the usersIDS String by , (comma)
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

In which senderUid is the missing child. This child can have the value like 1505217176288_myGroupName_1 or other coresponding group names.

Upvotes: 1

Related Questions