简体   繁体   English

如何检查我的Firebase数据库是否具有特定用户的数据? (Android / Java)

[英]How can I check if my Firebase database has data for a certain user? (Android/Java)

When a user makes an account with firebase on my android app, they then need to be directed to a screen where they can add some separate information like username, for example. 当用户在我的android应用上使用firebase进行帐户注册时,需要将他们定向到可以添加一些单独信息(例如用户名)的屏幕。 The app knows to switch the activity through an AuthStateListener, so when an account is made or someone signs in the code here is executed (no intents are changed here right now, keep reading to see why): 该应用程序知道可以通过AuthStateListener来切换活动,因此当创建帐户或执行有人在此处登录代码时(此时,这里的意图都没有改变,请继续阅读以了解原因):

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    userID = user.getUid();
                    toastMessage("User has signed in: " + user.getEmail());


                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };

In the if block where it is checking to see if user does not equal to null, this is where I need to change the intent to another activity where they will enter their username. 在if块中,它检查用户是否等于null,这是我需要将意图更改为另一个活动的地方,他们将在其中输入用户名。 Basically the logic is, if the user already has a username and they are just signing in then they will be directed to the home screen activity, and if they are signing up they need to be directed to the activity where they make a username. 基本上,逻辑是,如果用户已经有一个用户名并且正在登录,那么他们将被定向到主屏幕活动,如果他们正在注册,则需要被定向到创建用户名的活动。 To know which activity I need to send them to I need to have the code look in the firebase database and see if their account has a username already or not. 要知道我需要将其发送给哪个活动,我需要在firebase数据库中查找代码,并查看他们的帐户是否已经具有用户名。 I cannot figure out how to read data from the firebase in this situation, it seems as if the only way to read from the database (according to others) is through an onDataChange methods like so: 在这种情况下,我无法弄清楚如何从Firebase读取数据,似乎从数据库读取数据的唯一方法(根据其他方法)似乎是通过onDataChange方法,如下所示:

        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d(TAG, "This: "+dataSnapshot.getValue());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

which uses dataSnapshots. 使用dataSnapshots。 the problem with this is this code is only fired when data is added to the database, therefor this method will not execute any code when a user is simply signing up and has never added a username to the database. 问题在于,仅当将数据添加到数据库时才触发此代码,因此,当用户只是注册并且从未向数据库添加用户名时,此方法将不会执行任何代码。 How can I query to see data from the database just to check if a certain user has any data there? 我该如何查询以查看数据库中的数据,只是检查某个用户那里是否有任何数据?

OnDatachange is fired in all of cases, if user add data, remove data, change data ect. 在所有情况下都会触发OnDatachange ,如果用户添加数据,删除数据,更改数据等。

To check is user already have username do this inside OnDatachange : 要检查用户是否已有用户名,请在OnDatachange内部执行以下操作

if(dataSnapshot.exists()){
//GO TO OTHER ACTIVITY
]else{
//GO TO SET USER DATA ACTIVITY
]

See this exemple of one of my implementations: 请参阅我的一种实现的示例:

if (user !=null){
            DatabaseReference myRef = database.getReference("users").child(user.getUid()).child("user_bio");

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        bio.setText(dataSnapshot.getValue(String.class));

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

In this exemple if user_bio exists then set text to userbio if not do nothing. 在此示例中,如果user_bio存在,则将文本设置为userbio,如果不执行任何操作。 In the first time that user log in in my app this method do nothing but when user add bio this method set the bio text in userbio text. 第一次用户登录我的应用程序时,此方法什么也不做,但是当用户添加bio时,此方法会在userbio文本中设置bio文本。 but if i want to show some text if user hasnt bio i simply add na else and set text to the bio for exemple: 但是,如果我想在用户没有简历的情况下显示一些文本,我只需添加其他代码,然后将文本设置为简历,例如:

if (user !=null){
            DatabaseReference myRef = database.getReference("users").child(user.getUid()).child("user_bio");

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        bio.setText(dataSnapshot.getValue(String.class));
                    }else{
                        bio.setText("no user bio");
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

Have you try to use addValueEventListener inside onCreate method? 您是否尝试在onCreate方法中使用addValueEventListener?

If you run an activity while the listener inside onCreate method it will fired once when you open an activity. 如果在onCreate方法中的侦听器中运行活动,则在打开活动时将触发一次。 So i think you shouldn't have a problem here. 所以我认为您在这里应该不会有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户是否已在firebase身份验证和firebase实时数据库中的数据之间注册和匹配数据? - How can I check user had registered and match data between data on firebase authentication and firebase Realtime Database? 如何检查文件中是否包含用户输入的特定数字? (Java)的 - How can I check if a file contains a certain number that the user has input? (java) 如何检查 firebase 数据库中是否有值? - How can I check if a value is there in my firebase database? 如何检查 firebase 数据库中的愿望清单数据是否为空 - How can I check wishlist data is empty or not in firebase database 显示来自特定用户的数据(Android Studio Java编程+ Firebase) - Showing data from certain user (android studio java programming + firebase) 如何将 Arraylist 数据写入 Java 中的 Firebase 数据库? - How can I write Arraylist data to Firebase Database in Java? 我怎样才能检查我的sqlite表中是否有数据? - How can i check to see if my sqlite table has data in it? 如何使用我未使用的特定用户名和密码向用户授予对我的数据库的访问权限? - How can I grant access of my database to a user with a certain username and password which I am not using? 如何检查 Firebase 数据库中是否存在某个值 - How to check if a certain value is there in a Firebase database 如何检查当前用户是否在 Firebase (Android Java) 上通过身份验证 - How to check if the current user is authenticated on Firebase (Android Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM