Skip to content Skip to sidebar Skip to footer

How To Read / Query Data From One Userid Firebase Android

I want to retrieve data for a given user ID, which I am storing as a value in the database under each user. My database structure looks like below: user: { UserID: { u

Solution 1:

Try Using addChildEventListener and Query like following..

mReference.child("user").orderByChild("userId").equalTo(user.getUid()).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        //deal with data object
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

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

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

OR use addValueEventListener

mReference.child("user").orderByChild("userId").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

             //deal with data object

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            //toastMsg(databaseError.getMessage());Log.e(TAG, "onCancelled: " + databaseError.getMessage());
        }
    });

Solution 2:

Use something like the below code which i am using in my application and it works -

publicstaticvoidstoragesSetup(final Context context){

        if(!hasSignedInUser()) {
            **your business logic here**
            return;
        }

        // Write a message to the databasefinalDatabaseReferencedatabaseRef= FirebaseDatabase.getInstance()
                .getReference("cart").child(getCurrentUser().getUid());

        // Read from the database
        databaseRef.addValueEventListener(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again// whenever data at this location is updated.

                **put your business logic here**

            }

            @OverridepublicvoidonCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
    }

Solution 3:

A Firebase Database query inspects the child nodes directly under the location that you execute it on. If you want to order on a child property of those nodes (i.e. use orderByChild()), that property must exist at a fixed path under each child nodes.

In your case, you have two nested dynamic children, which means you:

  • can query a known user for their results ordered by score
  • can't query across all users for their results ordered by score

If you want to allow the latter query, you'll need to change your data model to allow it. For example, by keeping a list of all results across all players:

ResultsTable:{
    "-KbJtw467nl6p253HZ537" : {
      "Name" : "name1",
      "Email" : "something1@something.com",
      "userid" : "fmXWU324VHdDb8v6h7gvNjRojnu33"
    },
    "-Kb012ls9iMnzEL723o9I" : {
      "Name" : "name2",
      "Email" : "something2@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0aq0q25FJq38256SrC" : {
      "Name" : "name3",
      "Email" : "something3@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0atopfK64F6jy124Qi1u" : {
      "Name" : "name3",
      "Email" : "something1@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    }
}

Solution 4:

Please get a idea from below code.

Order objects:

Order objects

DatabaseReferencemDatabaseReference= FirebaseDatabase.getInstance()
                .getReference();
DatabaseReferencenode= mDatabaseReference.child("order")

node.orderByChild("timestamp").equalTo("-KZFQLVAv_kARdfnNnib")
                   .addValueEventListener(mValueEventListener);

privateValueEventListenermValueEventListener=newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
            Log.i(TAG, "onDataChange: " + snapshot.getValue());
            // Your code
        }

        @OverridepublicvoidonCancelled(DatabaseError error) {
            Log.w(TAG, "onCancelled: " + error.getMessage());
        }
    };

This return the first order object. Hope you get the point Thanks! :)

Post a Comment for "How To Read / Query Data From One Userid Firebase Android"