本文介绍了如何检查Firebase数据类Anroid中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查Firebase数据库中是否已经存在公交车号。

以下是我的示例代码。我一直在寻找过去的日子,但我找不到合适的代码。

  ref = new Firebase Config.FIREBASE_URL); 
postRef = ref.child(BusNumber);

busNum = edtBus.getText()。toString()。trim();
route1 = route.trim();
seat = edtSe​​at.getText()。toString()。trim(); $!
$ b if(!busNum.isEmpty()&&!route1.isEmpty()&&!seat.isEmpty()){
postRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.child(busNum).exists()){
edtBus.setError(Bus number already exists 。);
;
;其他$ {
busNumber = new BusNumber();
busNumber.setBusNum(busNum);
busNumber .setRoute(route1);
busNumber.setNumSeat(seat);
postRef.push()。setValue(busNumber);
edtBus.setText();
edtSe​​at。 setText();
Toast.makeText(AddBusActivity.this,Saving successful!,Toast.LENGTH_SHORT).show();


$ b @Override
public void onCancelled(FirebaseError firebaseError){
Toast.makeText(AddBusActivity.this,Error,Toast。 LENGTH_SHORT).show();
Toast.makeText(AddBusActivity.this,firebaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});

} else {
Toast.makeText(AddBusActivity.this,Please complete the information,Toast.LENGTH_SHORT).show();
}

有人可以帮我解决这个问题吗?提前致谢。
if语句是否正确,我的问题是为什么 postRef.addListenerForSingleValueEvent ...不起作用?我尝试测试一些吐司消息,但消息不会弹出。

解决方案

您的方法是错误的。

当您执行 dataSnapshot.child(busNum).exists(),它正在关键部分查找 busNum ,其中的键是 -kasajdh ...



所以你可以做的是获得 itrable 现在当你寻找
data.child(busNum).exists()时,它与值相关
$ (DataSnapshot数据)
$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ :dataSnapshot.getChildren()){
if(dat a.child(busNum).exists()){
// do ur stuff
} else {
//做某事
}
}
}
$ b $ @覆盖
public void onCancelled(FirebaseError firebaseError){

$ b});


I want to check if the bus number already exist in the database of Firebase.

Here's my sample code. I've been searching the past days but I can't find the right code to do so.

ref = new Firebase(Config.FIREBASE_URL);
            postRef = ref.child("BusNumber");

            busNum = edtBus.getText().toString().trim();
            route1 = route.trim();
            seat = edtSeat.getText().toString().trim();

            if (!busNum.isEmpty() && !route1.isEmpty() && !seat.isEmpty()) {
                postRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.child(busNum).exists()) {
                            edtBus.setError("Bus number already exists.");
                            edtBus.setText("");
                        } else {
                            busNumber = new BusNumber();
                            busNumber.setBusNum(busNum);
                            busNumber.setRoute(route1);
                            busNumber.setNumSeat(seat);
                            postRef.push().setValue(busNumber);
                            edtBus.setText("");
                            edtSeat.setText("");
                            Toast.makeText(AddBusActivity.this, "Saving successful!", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        Toast.makeText(AddBusActivity.this, "Error", Toast.LENGTH_SHORT).show();
                        Toast.makeText(AddBusActivity.this, firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

            } else {
                Toast.makeText(AddBusActivity.this, "Please complete the information", Toast.LENGTH_SHORT).show();
            }

Can somebody help me with this matter? Thanks in advance.Whether the if statement is correct or not, my also problem is why does the postRef.addListenerForSingleValueEvent...doesn't work? I tried to test some toast message but the message won't pop out.

解决方案

Your approach is wrong.

When you are doing this dataSnapshot.child(busNum).exists(), it's looking for the busNum in the key section, where your keys are -kasajdh....

So instead what you can do is, get the itrable, now when you look fordata.child(busNum).exists() it relates to the value

   postRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    if (data.child(busNum).exists()) {
                        //do ur stuff
                    } else {
                       //do something
                    }
                  }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });

这篇关于如何检查Firebase数据类Anroid中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:40