Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview

ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView

The item view type you are returning from

getItemViewType() is >= getViewTypeCount().

Using Multiple Views in a Custom Listview Adapter

You need to use the model class below

import java.io.Serializable;

public class ListModel implements Serializable {

private String name;
private String address;
private String image;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}
}

And in your java class, u need to inflate the data like this

ArrayList<ListModel> arrayList = new ArrayList<>();

for (int i = 0; i <10; i++) {

ListModel model = new ListModel();

model.setName("Name " + i);
model.setAddress("Address " + i);
model.setImage("Image " + i);

arrayList.add(model);
}

// YOU CAN USE THIS LIST IN YOUR ADAPTER JUST PASS YOUR ARRAYLIST INTO ADAPTER
CustomCurationAdaptor adapter = new CustomCurationAdaptor(this, arrayList);

ArrayAdapter and ListView - ArrayIndexOutOfBoundsException

What version of Android are you running? From what I can tell your Adapter is returning the wrong count from getViewTypeCount() (or you are changing the view type count dynamically which is a big mistake.)

ViewHolder ArrayIndexOutOfBoundsException: length=2; index=2

Replace

@Override
public int getItemViewType(int pos) {
if (pos < friends.size())
return 1;
return 2;
}

by

@Override
public int getItemViewType(int pos) {
if (pos < friends.size())
return 0;
return 1;
}

From the Adapter documentation for the method getItemViewType (my emphasis):

Returns

An integer representing the type of View. Two views should share the same type if one can be converted to the other in getView(int, View, ViewGroup). Note: Integers must be in the range 0 to getViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

At the moment, you are returning values 1 and 2 for the view type. You need to return values 0 and 1 instead to satisfy the condition in bold.

ArrayIndexOutOfBoundsException when using ListAdapter

Found the problem. View types had to begin at 0, mine starts at 1.

Android is using view types I provided to access an array by index somewhere. SomeArraySomewhere[myViewType] The size of this array probably equals the number of view types. So this means I have to start my view types at 0.

Multiple ListViews with multiple instances of the same custom adapter in one activity

The list in your adapter is static, so it's the same for all the instances of the class.

Replace public static List<Run> list = null; with public List<Run> list = null;

I would also advise you to make it private instead of public (and create a getter if you need to access it from outside the class).



Related Topics



Leave a reply



Submit