迭代嵌套的Firebase数据库节点

时间:2017-01-23 08:52:32

标签: android firebase firebase-realtime-database

我正在尝试迭代具有以下数据快照的嵌套Firebase数据库:

 DataSnapshot { key = Presidents, value = {1={aspirantName=Uh Ky, thumbnail=434, currentVotes=324595}, position=1, 2={aspirantName=Rao O, thumbnail=3, currentVotes=32}, numberofAspirants=3, docketName=Presidents} }

但是得到这个例外:

  

com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.esir.jualeader.Aspirant

类型

datasnapshot是一个Log from

private void getAspirantsData(DataSnapshot dataSnapshot) {
    aspirantsArrayList.clear(){
    Log.e("Ds", "" + dataSnapshot); //Prints  DataSnapshot { key = Presidents, value = {1={aspirantName=Uh Ky, thumbnail=434, currentVotes=324595}, position=1, 2={aspirantName=Rao O, thumbnail=3, currentVotes=32}, numberofAspirants=3, docketName=Presidents} }
    Iterable<DataSnapshot>iterable=dataSnapshot.getChildren();
    Iterator<DataSnapshot>iterator=iterable.iterator();
    while (iterator.hasNext()){
Aspirant aspirant=iterator.next().getValue(Aspirant.class); // com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.esir.jualeader.Aspirant
        Log.e("Aspirant ", "" + aspirant.getAspirantName()+" "+aspirant.getCurrentVotes()+" "+aspirant.getThumbnail());//This Works therefore Aspirant Object has Been Created
        aspirantsArrayList.add(aspirant);
    }
}

任何人都可以协助解决异常的原因吗?它试图将哪个字符串转换为我的Aspirant对象。

这是我的Aspirants Class

public class Aspirant {
    private String aspirantName;
    private int currentVotes;
    private int thumbnail;

    public Aspirant(){

    }
    public Aspirant(String aspirantName, int currentVotes, int thumbnail){
        this.aspirantName=aspirantName;
        this.currentVotes=currentVotes;
        this.thumbnail=thumbnail;
    }

    public String getAspirantName() {
        return aspirantName;
    }

    public void setAspirantName(String aspirantName) {
        this.aspirantName = aspirantName;
    }

    public int getCurrentVotes() {
        return currentVotes;
    }

    public void setCurrentVotes(int currentVotes) {
        this.currentVotes = currentVotes;
    }

    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }
}

以下是我的Firebase数据库结构

{
    "Presidents" : {
      "1" : {
        "aspirantName" : "Urhu atta",
        "currentVotes" : 324595,
        "thumbnail" : 434
      },
      "2" : {
        "aspirantName" : "Rla Oga",
        "currentVotes" : 32,
        "thumbnail" : 3
      },
      "docketName" : "Presidents",
      "numberofAspirants" : 3,
      "position" : 1
    },
    "Senetors" : {
      "docketName" : "Senetors",
      "numberofAspirants" : 5,
      "position" : 2
    }
}

2 个答案:

答案 0 :(得分:1)

您的JSON中存在不兼容的数据。在President下,您有两个有效的Aspirant个对象:

  "1" : {
    "aspirantName" : "Urhu atta",
    "currentVotes" : 324595,
    "thumbnail" : 434
  },

  "2" : {
    "aspirantName" : "Rla Oga",
    "currentVotes" : 32,
    "thumbnail" : 3
  },

但是你也有这些属性,其中任何一个都无法转换为Aspirant

  "docketName" : "Presidents",
  "numberofAspirants" : 3,
  "position" : 1

这是Firebase文档建议不要在公共根目录下嵌套不同类型的数据的众多原因之一。更好的数据结构是:

"Dockets": {
    "Presidents" : {
      "docketName" : "Presidents",
      "numberofAspirants" : 3,
      "position" : 1
    },
    "Senetors" : {
      "docketName" : "Senetors",
      "numberofAspirants" : 5,
      "position" : 2
    }
},
"Candidates": {
    "Presidents" : {
      "1" : {
        "aspirantName" : "Urhu atta",
        "currentVotes" : 324595,
        "thumbnail" : 434
      },
      "2" : {
        "aspirantName" : "Rla Oga",
        "currentVotes" : 32,
        "thumbnail" : 3
      }
    },
    "Senetors" : {
      ... nothing here yet
    }
}

现在,您可以安全地从Aspirant键下读取所有Candidates个对象。

答案 1 :(得分:0)

尝试用这个替换整个“getAspirantsData”函数:

private void getAspirantsData(DataSnapshot dataSnapshot) 
{
aspirantsArrayList.clear()
Log.e("Ds", "" + dataSnapshot);
for(DataSnapshot snap : datasnapshot.getChildren()
    aspirantsArrayList.add(snap.getValue(Aspirant.class);
}