how to display logged in user data from Fire base Real time database in android studio

Einstein :

Based on logged in user i want to display their information like name, address, bloodgroup etc. My database structure is like the below.

Donor:

 O+:
      einsein:
           name: einstein
           email: [email protected]
           bloodgroup: O+
           age: 20
           address: 11/237
 A-:
      thamizh:
           name: thamizh
           email: [email protected]
           bloodgroup: A-
           age: 21
           address: 11/23.
public class userinfo extends AppCompatActivity {

    TextView t1,t2,t3,t4,t5;
    DatabaseReference oref;
    FirebaseAuth auth;
    FirebaseUser user;
    String uid,email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);

        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        uid = user.getUid();
        email = user.getEmail();
        t1 = (TextView) findViewById(R.id.l11);
        t2 = (TextView) findViewById(R.id.l22);
        t3 = (TextView) findViewById(R.id.l33);
        t4 = (TextView) findViewById(R.id.l44);
        t5 = (TextView) findViewById(R.id.l55);

        oref = FirebaseDatabase.getInstance().getReference().child("Donor");
        oref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String  name = dataSnapshot.child("donorID").getValue().toString();
                String  bldgrp = dataSnapshot.child("bloodgroup").getValue().toString();
                String  mob = dataSnapshot.child("mobile").getValue().toString();
                String  eid = dataSnapshot.child("email").getValue().toString();
                String  add = dataSnapshot.child("address").getValue().toString();
                t1.setText(name);
                t2.setText(bldgrp);
                t3.setText(mob);
                t4.setText(eid);
                t5.setText(add);

                }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}
Frank van Puffelen :

Right now your code is loading all users from the database, and then tries to read the properties for one of those users from the /Donor node. That won't work.

At the very least you'll need to loop over the users in your onDataChange:

oref = FirebaseDatabase.getInstance().getReference().child("Donor");
oref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshots) {
        for (DataSnapshot dataSnapshot: snapshots.getChildren()) {
            String  name = dataSnapshot.child("donorID").getValue().toString();
            String  bldgrp = dataSnapshot.child("bloodgroup").getValue().toString();
            String  mob = dataSnapshot.child("mobile").getValue().toString();
            String  eid = dataSnapshot.child("email").getValue().toString();
            String  add = dataSnapshot.child("address").getValue().toString();
            t1.setText(name);
            t2.setText(bldgrp);
            t3.setText(mob);
            t4.setText(eid);
            t5.setText(add);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // never ignore errors
    }
});

The above code still gets all child nodes from /Donor, but now loops through them and sets the properties in the text view. Since you only have text views for one user, you are overwriting the previous values each time. So in the end you'll have the properties for the last user from the JSON. That's not what you want, but at least it's better than what you currently have.


Next up is loading only the data for the single user that you want to display. This requires that you can identify that single user.

The most common way to do this, is to store the data in your database under the UID of that user. So:

Users: {
  "uidOfUser1": { ... },
  "uidOfUser2": { ... },
  "uidOfUser3": { ... }
}

With this type of structure you can get a reference to the data for the currently signed in user with:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
oref = FirebaseDatabase.getInstance().getReference()
                       .child("Donor")
                       .child(uid);

If you add the listener to this reference, you can remove the loop we just added, since you're only loading data for one user.


That leaves the question: what to do if you didn't store the user info under their UID? In that case, I'd recommend first considering restructuring the data, as storing user data under UIDs is the idiomatic way to get fast and easy lookups.

But sometimes you really can restructure, and in that case you can perform a database query to find the node(s) for a user. To be able to use a query, you must know the value of one (preferably unique) property of the user.

Say that you know their email address. You can then get the node(s) for that email address with:

FirebaseDatabase.getInstance().getReference()
                .child("Donor")
                .orderByChild("email").equalTo("[email protected]");

When you attach your listener to this query, you may get multiple results again, so you need the same for loop that we added above.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409293&siteId=1