therealone
therealone

Reputation: 421

Android/Firebase - Can't convert object of type java.util.ArrayList to type **my object**

I'm trying to get the values of each node under "templates."

enter image description here

For example, "eght" (just random test name), contains an ArrayList of ArrayLists. So, ArrayList<.ArrayList>.

I just need to get the values once, so I'm using a single event listener:

mTemplateRef.addListenerForSingleValueEvent(
     new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot templateSnapshot : dataSnapshot.getChildren()){

               // I've tried a million different things here, but all result in the error
              // This is the error line
              MapFoo mapFoo = templateSnapshot.getValue(MapFoo.class);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                    // ...
                }
            });

I always end up with this:

Can't convert object of type java.util.ArrayList to type com.appname.template_editor.MapFoo

The last part being any object I try.

Here's MapFoo:

By the way, I"m using a Map because when I debug, the nodes seem to be in HashMap form. I've also tried a HashMap object with the same error. I've also tried several other objects to try to make it work with no success.

public class MapFoo {
public Map<String, ArrayList> mapList;

public MapFoo(){

}

public Map getMapList(){
    return mapList;
}

public MapFoo(Map<String, ArrayList> mapList){
    this.mapList = mapList;
}

}

Another variation I tried:

public class MasterListTemplateClass {

public String templateName;
public ArrayList<ArrayList> arrayList;

public MasterListTemplateClass() {
}

public MasterListTemplateClass(String templateName, ArrayList<ArrayList> arrayList) {
    this.templateName = templateName;
    this.arrayList = arrayList;
}

public String getName() {
    return templateName;
}

public ArrayList<ArrayList> getList() {
    return arrayList;
}

}

I just can't get any of this to work. I simply need the values in my database!

Upvotes: 1

Views: 5837

Answers (2)

therealone
therealone

Reputation: 421

Just remembered this post. Have found the answer. When you're working with maps in Firebase, if you have an int as a string (1 vs "1"), Firebase reads that as an int. Whether you inputted it as a string or not. So what I did was add "_key" to the numbers ("1_key") which forced Firebase to read it as a string and not an int. This fixed things right up

Upvotes: 1

ugur
ugur

Reputation: 3654

instead of number ids like 0,1,2 give them a name to be able to shape ur model as i am not sure if it is possible to make a model with number names.

e.g. name 0,1,2 as days, title, dimen respectively and create following model

public class MyModel{

    public String days;
    public String title;
    public String dimen;

    public MyModel(){

    }

    public String getDays(){
        return this.days;
    }

    public String getTitle(){
        return this.title;
    }

    public String getDimen(){
        return this.dimen;
    }
}

Then reach this model via 2 loops by calling 2 snapshot.getChildren()

 for(DataSnapshot templateSnapshot : dataSnapshot.getChildren()){
     for(DataSnapshot snap : templateSnapshot.getChildren()){


              MyModel model = snap.getValue(MyModel.class);
                    }
                }
...

Upvotes: 2

Related Questions