user7989906
user7989906

Reputation:

Retrieve user data from Firebase database

I have created an Android App By which user can register and login.When a user registration is successful a Name and value will created in my database for that User. Now I want to retrieve The Data For every User separately.

Here is my Database look:

Upvotes: 0

Views: 10544

Answers (4)

Shaifali Rajput
Shaifali Rajput

Reputation: 1279

Try this:

 mDatabase.child("ezzeearn").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
            String point = map.get("Points");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
});

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

To retrieve the current user's data from this structure, you need two things:

  1. to know the uid of the current user
  2. to then read the data from the database

In code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseDatabase.getInstance.getReference(uid).addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long points = dataSnapshot.child("Points").getValue(Long.class);
     }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

Upvotes: 1

Ashish P
Ashish P

Reputation: 3056

Try to use below method :

     public void getAllUsersFromFirebase() {  
    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child("ezzeearn");
    UserRef.keepSynced(true);
    UserRef.addValueEventListener(new ValueEventListener() {
        @Override  
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();

            while (dataSnapshots.hasNext()) {
                DataSnapshot dataSnapshotChild = dataSnapshots.next();
                String resultString = (String)dataSnapshotChild.getValue();


            }  

        }  

        @Override  
        public void onCancelled(DatabaseError databaseError) {
                // for handling database error            
        }  
    });  
}  

Upvotes: 0

Jaydeep Khambhayta
Jaydeep Khambhayta

Reputation: 5279

for using iterator you can get the data for specific user

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {
        // Something went wrong!
    }
}

Upvotes: 0

Related Questions