[英]How to retrieve firebase database for specific user only?
I am using Firebase Database for my android application.我正在为我的 android 应用程序使用 Firebase 数据库。 Login feature is available.
登录功能可用。 There is a bookmark/favourite option in the app.
应用程序中有一个书签/收藏夹选项。 After pressing the bookmark button the data are saving with the user id separately.
按下书签按钮后,数据将与用户 ID 分开保存。 Exactly what I want.
正是我想要的。 But the problem is when I am trying to retrieve the saved data in recycleview they are showing all user's data instead of the user logged in. Please help me.
但问题是当我尝试在 recycleview 中检索保存的数据时,它们显示的是所有用户的数据,而不是登录的用户。请帮助我。 I am using cardview in recycleview.
我在 recycleview 中使用 cardview。
mUserDatabase = FirebaseDatabase.getInstance().getReference("Bookmarks"); mUserDatabase.keepSynced(true);
It is my Database for Idiom Phrase这是我的成语短语数据库
And it is the data added by user by pressing bookmark button这是用户按书签按钮添加的数据
You need to access the user uid, so only that user's data will be shown:您需要访问用户 uid,因此只会显示该用户的数据:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userId=user.getUid();
//String.valueOf(int)
mUserDatabase = FirebaseDatabase.getInstance().getReference("Bookmarks").child(String.valueOf(10)).child(userId);
getUid()
will retrieve the id of the current logged in user. getUid()
将检索当前登录用户的 id。
To retrieve specific users data you need to provide that user's id, try this code it might help you要检索您需要提供该用户 ID 的特定用户数据,请尝试使用此代码可能对您有所帮助
mUserDatabase = FirebaseDatabase.getInstance().getReference("Bookmarks").child("10");
Query query = mUserDatabase.child(userid);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//your code
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
This is not a good way of storing the favorites for each user.这不是为每个用户存储收藏夹的好方法。 Instead, change your database structure to:
相反,将您的数据库结构更改为:
BookMarks
|UserUID
|10
|3
|UserUID2
|6
|10
This structure will help you to view all the favorites of one user at a particular time.此结构将帮助您在特定时间查看一个用户的所有收藏夹。 Your current structure will limit you to viewing one favorited item for one user or all users who have favorited that item
您当前的结构将限制您查看一位用户或所有收藏该项目的用户的收藏项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.