如何从Firebase中按值获取数据?

时间:2019-07-18 05:22:59

标签: java android firebase

当我有两个孩子时,如何从Firebase调用值? 如果值是Guru,则转到DashboardGuru 如果值是Murid,则转到DashboardMurid Here Firebase Database 我只是得到空值

这是我的代码

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

                                                }
                                            });

2 个答案:

答案 0 :(得分:0)

我建议让一个孩子像sample users database child一样拥有所有用户

然后,当您要按类别选择每个类别时:

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();
                        }
                    }

答案 1 :(得分:0)

dataSnapshot.child(“ Guru”)。getValue()。toString() 搜索名为“ Guru”的键,该键在用户的ID下不存在于JSON中。因此它返回null值。从特权是否为Guru的问题开始,然后转到DashboardGuru活动,如果特权为Murid,则转到DashboardMurid。因此,更新后的代码应为:

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