1

My plan is to create a user UI for the users where they can view their own information when they register on the application and logged in.

Example: When User A, registers on the application then the information would automatically be inputted in the firebase database, then after that when they logged in only their own information(ex. Name, Email, Date) would be shown.

User

And from the picture above the textview would be replaced by their specific data on the database.

Here is my firebase database User firebase

The unique id on the users is directly fetched from firebase auth users-uid when they register on the application.

enter image description here

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Yamiess
  • 251
  • 4
  • 15
  • 1
    Please see https://stackoverflow.com/questions/48831813/organize-firebase-database-for-individual-users-idea – Arnav Rao Feb 20 '18 at 14:26

2 Answers2

3

Yes it is possible, if you have this in your database:

users
 useruid 
  name: peter
  gender: male

Then in android you can do this:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();

the above will retrieve the useruid from the database, then you can do a query:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("user").child(useruid);

and only data of that user will be in the activity..

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

After authentication, at first you have to fetch current user Uid.

String currentUid = FirebaseAuth.getInstance().getCurrentUser().getUid();

If this Uid is existed in firebase database it will fetch its child data and only that user data will be shown in the activity.

DatabaseReference uDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

uDatabaseReference.child(currentUid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        u_name = dataSnapshot.child("name").getValue().toString();
        u_phone = dataSnapshot.child("email").getValue().toString();
        u_date= dataSnapshot.child("date").getValue().toString();
        u_type= dataSnapshot.child("type").getValue().toString();
        Toast.makeText(ProfileActivity.this, "name: "+ u_name + "\nemail: "+ u_email + "\ndate: " + u_date + "\ntype: " + u_type, Toast.LENGTH_SHORT).show();
    }

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

    }
});