This may be duplicate Question but i have tries alot but i didnt get my result, Here is my firebase structure
Updated
I need to find if the child expensesName(For example it can be oil) is exists are not under entire node of November_2018(Inside red box).If exists i need to display toast
Below is the Code I have tried:
//Here monthyr is the November_2018
expensesaddref = databaseReference.child(username).child("Expense_Month").child(monthyr);
//Variable expensesname is the variable that i want to checked for existent
expensesaddref.orderByChild("expensesName").equalTo(expensesname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.d("List Exp","1 loop");
for(DataSnapshot data : ds.getChildren()){
Log.d("List Exp","2 loop");
if(data.child("expensesName").exists()){
Log.d("List Exp","if loop");
Toast.makeText(getContext(), "exist", Toast.LENGTH_SHORT).show();
}else
{
Log.d("List Exp","else loop");
Toast.makeText(getContext(), "not exist", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I dont know where iam wrong and it displays Not Exist Toast
always but the firebase has that value
As per PradyumanDixit Answer I have updated code but still its not working
//Here monthyr is the November_2018
expensesaddref = databaseReference.child(username).child("Expense_Month").child(monthyr);
expensesaddref.orderByChild("expensesName").equalTo("oil").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Toast.makeText(getContext(), "Exist", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(getContext(), "not", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…