Search code examples
androidtoast

Toast not working when data is not existing


My Toast not working when the data is not existing from Firebase database. What happen ?

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

        }
    });
}

Solution

  • It may be possible your query databaseReference.orderByChild("id").equalTo(id) this condition not able to getting any record so it will be not execute for loop so you may to check first the number of children are greater than 0 or not.

    You can try this way 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();
                      }