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
CSS Reference to Phone's Emoji Font
Automatic Versioning of Android Build Using Git Describe with Gradle
How to Have a Masked Numeric Input Field
Android - Expandable Textview with Animation
Find the Physical Address of Exception Vector Table from Kernel Module
Android - Multiple Custom Versions of The Same App
I'M Getting a Nullpointerexception When I Use Action_Image_Capture to Take a Picture
How to Pin a Certificate with Square Okhttp
Tracking Progress of Multipart File Upload Using Okhttp
Android Animate Drop Down/Up View Proper
How to Convert Date Format in Android
Onpagefinished() Never Called (Webview)!
Flutter: Upgrade The Version Code for Play Store
Supportmapfragment Does Not Support Androidx Fragment
I Want Show Notification at 8:00 Am Everyday
How to Show a Mediacontroller While Playing Audio in Android
A Pre-Populated Database Does Not Work at API 28 Throws "No Such Table" Exception