简体   繁体   English

如何从firebase按值获取数据?

[英]How to get data by value from firebase?

How do i call value from firebase when have two child?有两个孩子时如何从 firebase 调用价值? If value is Guru then go to DashboardGuru if value is Murid then go to DashboardMurid Here Firebase Database i just got null value如果值是 Guru 然后转到 DashboardGuru 如果值是 Murid 然后转到 DashboardMurid这里 Firebase 数据库我刚得到空值

Here's my code这是我的代码

 db.getReference().child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                                @Override
                                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                                    Common.currentUser = dataSnapshot.getValue(Pengguna.class);
                                                    waitingDialog.dismiss();
                                                    String priv = dataSnapshot.child("Guru").getValue().toString();
                                                    if(priv.equals("Guru")){
                                                    startActivity(new Intent(MainActivity.this, DashboardGuru.class));
                                                    finish();
                                                    }
                                                    else{
                                                        startActivity(new Intent(MainActivity.this, DashboardMurid.class));
                                                        finish();
                                                    }
                                                }

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

                                                }
                                            });

I would suggest having one child that holds all the users like我建议让一个孩子拥有所有喜欢的用户示例用户数据库子项

Then when you want to select each by category you:然后,当您想按类别选择每个时,您:

query = databaseReference.child("users").orderByChild("userID")
                        .equalTo(user.getUid());

                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()){
                            for (DataSnapshot userDetails : dataSnapshot.getChildren()){
                                UserAccount userAccount =userDetails.getValue(UserAccount.class);

                                assert userAccount != null;
                                if(userAccount.userCategory.equals("Fisher Man")){
                                    Intent startActivityForFisherMan = new Intent(LoginActivity.this, FisherManActivity.class);
                                    startActivity(startActivityForFisherMan);
                                    finish();
                                }else{
                                    Intent startActivityForCustomer = new Intent(LoginActivity.this, CustomerMainActivity.class);
                                    startActivity(startActivityForCustomer);
                                    finish();
                                }
                            }

                        }else{
                            Toast.makeText(LoginActivity.this, "Invalid User: Kindly Register", Toast.LENGTH_SHORT).show();
                        }
                    }

dataSnapshot.child("Guru").getValue().toString() searches for a key named " Guru " which does not exist in the JSON under an id of a user. dataSnapshot.child("Guru").getValue().toString() 搜索名为“ Guru ”的键,该键在用户 ID 下的 JSON 中不存在。 so it returns null value .所以它返回空值。 From the question if the privilege is Guru then go to DashboardGuru activity, if the privilege is Murid goto DashboardMurid.从特权是否为 Guru 的问题中,转到 DashboardGuru 活动,如果特权为 Murid,则转到 DashboardMurid。 So the updated code should be :所以更新后的代码应该是:

String priv = dataSnapshot.child("privilege").getValue().toString();

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

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