Reputation: 2249
I am trying to iterate a nested Firebase database that has the following datasnapshot:
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} }
But get this exception:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.esir.jualeader.Aspirant
the datasnapshot is a 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);
}
}
Can anyone assist on why the exception is occurring? And which string is it trying too convert into my Aspirant object.
This is my 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;
}
}
The following is my Firebase database structure
{
"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
}
}
Upvotes: 0
Views: 247
Reputation: 599176
You have incompatible data in your JSON. Under President
you have two valid Aspirant
objects:
"1" : {
"aspirantName" : "Urhu atta",
"currentVotes" : 324595,
"thumbnail" : 434
},
And
"2" : {
"aspirantName" : "Rla Oga",
"currentVotes" : 32,
"thumbnail" : 3
},
But you also have these properties, none of which can be converted to a Aspirant
:
"docketName" : "Presidents",
"numberofAspirants" : 3,
"position" : 1
This is one of the many reasons the Firebase documentation recommends against nesting different types of data under a common root. A better structure for your data would be:
"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
}
}
Now you can safely read all Aspirant
objects from under the Candidates
key.
Upvotes: 1
Reputation: 1925
Try to replace your whole "getAspirantsData" function with this one:
private void getAspirantsData(DataSnapshot dataSnapshot)
{
aspirantsArrayList.clear()
Log.e("Ds", "" + dataSnapshot);
for(DataSnapshot snap : datasnapshot.getChildren()
aspirantsArrayList.add(snap.getValue(Aspirant.class);
}
Upvotes: 0