2017-01-23 53 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' –