在Firebase Android中将值添加到不同节点的同一列表中

时间:2017-02-21 14:24:44

标签: android list firebase firebase-realtime-database

我有以下Firebase数据库结构

 "Aspirant" : {
    "Laikipia County" : {
      "Senators" : {
        "Esir Makau" : {
          "aspirantCounty" : "Laikipia County",
          "aspirantDocket" : "Senators",
          "aspirantName" : "Esir Makau",
          "aspirant_twitter_handle" : "esir",
          "currentVotes" : 62,
          "thumbnail" : "dd",
          "votes" : {
            "111" : true,
            "358110075338850" : true
          },
          "votesCount" : 0
        },
        "JoMueke" : {
          "aspirantCounty" : "Laikipia County",
          "aspirantDocket" : "Senators",
          "aspirantName" : "JoMueke",
          "aspirant_twitter_handle" : "jmueke",
          "currentVotes" : 344,
          "thumbnail" : "trued",
          "votes" : {
            "111" : true
          },
          "votesCount" : 0
        },
        "Mionko" : {
          "aspirantCounty" : "Laikipia County",
          "aspirantDocket" : "Senators",
          "aspirantName" : "Mionko",
          "aspirant_twitter_handle" : "MikeSonko",
          "currentVotes" : 6620,
          "thumbnail" : "http://venasnews.co.ke/wp-content/uploads/2016/10/mike-sonko.jpg",
          "votes" : {
            "111" : true
          },
          "votesCount" : 0
        }
      }
    },
    "Nairobi County" : {
      "Senators" : {
        "Jonathan Mueke" : {
          "aspirantCounty" : "Nairobi County",
          "aspirantDocket" : "Senators",
          "aspirantName" : "Jonathan Mueke",
          "aspirant_twitter_handle" : "jmueke",
          "currentVotes" : 3462,
          "thumbnail" : "trued",
          "votes" : {
            "22" : true,
            "358110075338850" : true
          },
          "votesCount" : 0
        },
        "Mike Sonko" : {
          "aspirantCounty" : "Nairobi County",
          "aspirantDocket" : "Senators",
          "aspirantName" : "Mike Sonko",
          "aspirant_twitter_handle" : "MikeSonko",
          "currentVotes" : 6622,
          "thumbnail" : "http://venasnews.co.ke/wp-content/uploads/2016/10/mike-sonko.jpg",
          "votes" : {
            "22" : true
          },
          "votesCount" : 0
        }
      }
    }
  },
  "Aspirants" : {
    "Presidents" : {
      "Musalia Mudavadi" : {
        "aspirantCounty" : "Kenya County",
        "aspirantDocket" : "Presidents",
        "aspirantName" : "Musalia Mudavadi",
        "aspirant_twitter_handle" : "MusaliaMudavadi",
        "currentVotes" : 1220,
        "thumbnail" : "http",
        "votes" : {
          "111" : true
        },
        "votesCount" : 0
      },
      "Raila Odinga" : {
        "aspirantCounty" : "Kenya County",
        "aspirantDocket" : "Presidents",
        "aspirantName" : "Raila Odinga",
        "aspirant_twitter_handle" : "RailaOdinga",
        "currentVotes" : 1047,
        "thumbnail" : "http://www.somalilandpress.com/wp-content/uploads/2016/07/raila.jpg",
        "votes" : {
          "111" : true,
          "358110075338850" : true
        },
        "votesCount" : 0
      },
      "Uhuru Kenyatta" : {
        "aspirantCounty" : "Kenya County",
        "aspirantDocket" : "Presidents",
        "aspirantName" : "Uhuru Kenyatta",
        "aspirant_twitter_handle" : "UKenyatta",
        "currentVotes" : 1043,
        "thumbnail" : "http://hashtagsquare.co.ke/wp-content/uploads/2010/08/uhuru-kenyatta-biography.jpg",
        "votes" : {
          "111" : true
        },
        "votesCount" : 0
      }
    }
  }

我想要阅读的两个数据库节点是

availableAspirantsPresidents=FirebaseDatabase.getInstance().getReference().child("Aspirants").child("Presidents");
availableAspirants= FirebaseDatabase.getInstance().getReference().child("Aspirant").child(Counties[county]);

然后我有一个清单

final ArrayList<Aspirant>commonPresidentsAndSpecificAspirants=new ArrayList<>();

我想添加availableAspirantsPresidents的内容作为commonPresidentsAndSpecificAspirants ArrayList的第一个元素,即使availableAspirants的内容没有任何子节点;

目前我正在尝试按以下方式执行此操作

availableAspirants.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot dss:dataSnapshot.getChildren()){
                    Log.e("Key :",""+dss.getKey());//Prints eg. Senators,Governors etc
                    if(dss.getChildrenCount()>0){
                        //I want Even if dss.getChildrenCount=0 Still add a Pressident in the List
                        ArrayList<BarEntry>entries=new ArrayList<>();
                        for(int i=0;i<dss.getChildrenCount();i++){//Real Asp
                            Aspirant aspirant;
                            for (DataSnapshot fd:dss.getChildren()){
                                aspirant=fd.getValue(Aspirant.class);
                                commonPresidentsAndSpecificAspirants.add(aspirant);
                            }
                            if(commonPresidentsAndSpecificAspirants.size()>0){                                 
                                entries.add(new BarEntry(i, (float) (commonPresidentsAndSpecificAspirants.get(i).getCurrentVotes())));
                            }
                        }
                    }else{

                }
            }

如何将availableAspirantsPresidents的内容添加到commonPresidentsAndSpecificAspirants作为第一个元素。 我尝试嵌套availableAspirantsPresidents监听器,但它不起作用。 任何帮助将受到高度赞赏。

0 个答案:

没有答案