2017-01-23 70 views
0

我试图遍历具有以下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 
    } 
} 
+1

您可能想要分享您的“Aspirant”类,这可能是问题的原因。 –

+0

'公开课上访者私人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 –

+0

@EsirKings生成的,您应该编辑您的问题并将类的代码放到您的问题中。很难在单行中读取代码。 – Wilik

回答

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 
    } 
} 

现在可以安全地从Candidates项下读取所有Aspirant对象。

+0

Thanx。我以为我会使用一个节点,以便在那里存储有志的人数,并同时吸引有志的人。我按照你的建议完成了它,并且它正在工作。 –

0

尝试用这个来代替你的整个 “getAspirantsData” 功能:

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

仍然无法使用。我猜是因为我的Datasnapshot“docketName”中的三个非迭代字段:“总统”, “numberofAspirants”:3, “position”:1' –