查找子数据中的子对象是否存在,Android Firebase是否存在 [英] Find if child inside child data is exist or not Android Firebase

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

问题描述

这可能是重复的问题,但是我尝试了很多,但是没有得到结果,这是我的firebase结构

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

已更新

我需要确定子对象 expensesName (例如可以是 oil )是否存在于 2018年11月_的整个节点下 (在红色方框内).如果存在,我需要展示吐司面包

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

               }
           });

我不知道哪里出了错,它始终显示 Not Exist Toast ,但是firebase拥有该值

I dont know where iam wrong and it displays Not Exist Toast always but the firebase has that value

按照 PradyumanDixit答案,我已经更新了代码,但仍然无法正常工作

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

            }
        });

推荐答案

感谢您的所有回答 Alex,Aadil,Pradyuman .正如大家所建议的那样,我已将查询重组为 Aadil ,我可以根据需要查询我的所有数据.这是我的结构

Thank you for all your answer Alex,Aadil,Pradyuman .As you all suggested i have restructure my query as Aadil mentioned and i can query all my data as required .This is my structure

Expense_Month
   |
   --- November_2018
          |
          --- uniqueKey
          |     |
          |     --- expensesName: "gross"
          |     |
          |     --- total: 66
          |     |
          |     --- date_exp: "3_gross"
          |     |
          |     --- current_date: 3

          |
          --- uniquekey
                |
                --- expensesName: "oil"
                |
                --- total: 33
                |
                --- date_exp: "4_oil"
                |
                --- curent_date: 4

这篇关于查找子数据中的子对象是否存在,Android Firebase是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆