ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView android android

ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView


The item view type you are returning from

getItemViewType() is >= getViewTypeCount().


The accepted answer is correct. This is what I am doing to avoid the problem:

public enum FoodRowType {    ONLY_ELEM,    FIRST_ELEM,    MID_ELEM,    LAST_ELEM}@Overridepublic int getViewTypeCount() {    return FoodRowType.values().length;}@Overridepublic int getItemViewType(int position) {    return rows.get(position).getViewType();  //returns one of the above types}


Issue comes when getItemType value is wrong.This value should be an integer and should be from 0 to getViewTypeCount()-1.