数据不存在时吐司不工作

Toast not working when data is not existing

当 Firebase 数据库中不存在数据时,我的 Toast 无法正常工作。发生了什么?

public void searching(final String id){
    DatabaseReference databaseReference = FirebaseDatabase.getInstance()
            .getReference("Employee");
    Query query = databaseReference.orderByChild("id").equalTo(id);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot data: dataSnapshot.getChildren()){
                if(data.child("id").exists()) {
                    Employee employee = data.getValue(Employee.class);
                    hp.setText(employee.getPhoneNum());
                    address.setText(employee.getAddress());
                    fullName.setText(employee.getFullName());
                    Ic.setText(employee.getIcNum());
                    Sex.setText(employee.getSex());
                    emailVerify.setText(employee.getEmail());

                    getData.setVisibility(View.GONE);
                    update.setVisibility(View.VISIBLE);
                    fullName.setVisibility(View.VISIBLE);
                    Ic.setVisibility(View.VISIBLE);
                    tAddress.setVisibility(View.VISIBLE);
                    tPhone.setVisibility(View.VISIBLE);
                    hp.setVisibility(View.VISIBLE);
                    address.setVisibility(View.VISIBLE);
                    Sex.setVisibility(View.VISIBLE);

                }else{

                    Toast.makeText(getContext(),"Please Enter Correct Employee ID",
                            Toast.LENGTH_SHORT).show();

                    return;
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

dataSnapshot.getChildren() 为空时,您将不会进入 for 块,因此 Toast 不会出现。

可能你的查询databaseReference.orderByChild("id").equalTo(id)这个条件没有得到任何记录所以它不会执行for循环所以你可以先检查children的数量是否大于0或不。

你可以这样试试 Like :

    if (dataSnapshot.dataSnapshot.getChildrenCount() > 0){
    for(DataSnapshot data: dataSnapshot.getChildren()){
                    if(data.child("id").exists()) {
                        Employee employee = data.getValue(Employee.class);
                        hp.setText(employee.getPhoneNum());
                        address.setText(employee.getAddress());
                        fullName.setText(employee.getFullName());
                        Ic.setText(employee.getIcNum());
                        Sex.setText(employee.getSex());
                        emailVerify.setText(employee.getEmail());

                        getData.setVisibility(View.GONE);
                        update.setVisibility(View.VISIBLE);
                        fullName.setVisibility(View.VISIBLE);
                        Ic.setVisibility(View.VISIBLE);
                        tAddress.setVisibility(View.VISIBLE);
                        tPhone.setVisibility(View.VISIBLE);
                        hp.setVisibility(View.VISIBLE);
                        address.setVisibility(View.VISIBLE);
                        Sex.setVisibility(View.VISIBLE);

                    }else{

                        Toast.makeText(getContext(),"Please Enter Correct Employee ID",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                  }
                } else {
                        Toast.makeText(getContext(),"No data found.",
                                Toast.LENGTH_SHORT).show();
                  }