简体   繁体   English

如何仅为特定用户检索firebase数据库?

[英]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.

相关问题 从Firebase数据库中检索特定用户的数据 - Retrieve data of a specific user from a Firebase database 如何从 Firebase 实时数据库中检索具有 UID 的特定用户的特定数据 - How to retrieve specific data of a specific user with UID from a Firebase realtime database Android:从Firebase数据库检索特定用户的数据 - Android: Retrieve data of a specific user from a Firebase database 如何在firebase数据库中检索特定孩子的数据 - android studio/java? - How to retrieve data for a specific child in firebase database - android studio/ java? 如何从 Firebase 实时数据库中检索特定数据? - How to retrieve Specific Data from Firebase Realtime Database? 如何在 Android Studio 中从实时 Firebase 中检索特定用户 - How to retrieve specific user from realtime Firebase in Android Studio 如何使用 Android 在 Firebase 实时数据库中检索经过身份验证的用户的用户数据 - How to retrieve user data of authenticated users in Firebase Realtime Database with Android 如何在Firebase数据库中显示特定当前登录用户的数据? - How to display data of specific current logged in user in Firebase Database? 如何从 firebase 数据库中检索特定用户 ID 的数据 - How to retrive data to specific user id from firebase database 如何在Firebase实时数据库中为特定用户设置读写规则? - How to set the read and write rules for a specific user in Firebase Realtime Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM