我试图遍历具有以下datasnapshot嵌套火力地堡数据库:DatabaseException迭代嵌套火力地堡数据库节点
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
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对象。
这是我的追求者类
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;
}
}
以下是我的火力地堡的数据库结构
{
"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
}
}
您可能想要分享您的“Aspirant”类,这可能是问题的原因。 –
'公开课上访者私人String aspirantName; private int currentVotes; 私人字符串缩略图; public Aspirant(){ } public Aspirant(String aspirantName,int currentVotes,String thumbnail){ this.aspirantName = aspirantName; this.currentVotes = currentVotes; this.thumbnail = thumbnail;所有其他的getter和setter都是由Android studio –
@EsirKings生成的,您应该编辑您的问题并将类的代码放到您的问题中。很难在单行中读取代码。 – Wilik