Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically update view types of ListView

I've created a custom adapter implementation. I also have a few different view types in this adapter.

I need to replace some views of my adapter at runtime. Romain Guy says that "...changing the view type count dynamically which is a big mistake" (see ArrayAdapter and ListView - ArrayIndexOutOfBoundsException). What is the correct way of doing this as the view type count changes dynamically?

AdapterCode:

...
private List<Class<?>> itemTypes = new ArrayList<Class<?>>(20);

private void initList(List<Object> items) {
    itemTypes = new ArrayList<Class<?>>(20);
    positionSection = new SparseArray<Integer>(items.size());

    int count = 0;
    int sectionIndex = -1;
    for (Object object : items) {
        Class<?> currentClass = object.getClass();
        if (object instanceof String) {
            sectionIndex = count;
        }
        if (!itemTypes.contains(currentClass)) {
            itemTypes.add(currentClass);
        }
        positionSection.put(count, sectionIndex);
        count++;
    }
}
@Override
public int getItemViewType(int position) {
    return itemTypes.indexOf(items.get(position).getClass());
}

@Override
public int getViewTypeCount() {
    int size = itemTypes.size();
    return size == 0 ? 1 : size;
}

@Override
public void notifyDataSetChanged() {
    initList(items);
    super.notifyDataSetChanged();
}
...

I update my item type list after I've changed my data, but it does not work as intended.

The above code results in an ArrayIndexOutOfBounds exception:

10-21 20:18:16.231: E/AndroidRuntime(4475): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
10-21 20:18:16.231: E/AndroidRuntime(4475):     at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6437)
10-21 20:18:16.231: E/AndroidRuntime(4475):     at android.widget.ListView.layoutChildren(ListView.java:1559)
10-21 20:18:16.231: E/AndroidRuntime(4475):     at android.widget.AbsListView.onLayout(AbsListView.java:2102)
10-21 20:18:16.231: E/AndroidRuntime(4475):     at se.davison.sodatools.widget.SectionListView.onLayout(SectionListView.java:147)
10-21 20:18:16.231: E/AndroidRuntime(4475):     at android.view.View.layout(View.java:13754)
10-21 20:18:16.231: E/AndroidRuntime(4475):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
like image 559
Richard Avatar asked Nov 27 '22 18:11

Richard


1 Answers

Solved with suggestion from https://stackoverflow.com/users/493939/luksprog

Changed these methods and did not create a new instance of itemTypes in initList

@Override
public int getViewTypeCount() {
    return 20;
}
like image 186
Richard Avatar answered Dec 15 '22 23:12

Richard