简体   繁体   中英

Is it possible to show only the data on the specific user id on android firebase?

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.

用户

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

Here is my firebase database用户火力点

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

在此处输入图片说明

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..

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) {

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM