Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

java - Find if child inside child data is exist or not Android Firebase

This may be duplicate Question but i have tries alot but i didnt get my result, Here is my firebase structure

database

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...