Created
August 11, 2015 07:17
-
-
Save evasyuk/a4532d697617a074145f to your computer and use it in GitHub Desktop.
ListView with ITEM_TYPE_COUNT > 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView | |
Romain Guy said: | |
The item view type you are returning from | |
getItemViewType() is >= getViewTypeCount(). | |
from http://stackoverflow.com/questions/2596547/arrayindexoutofboundsexception-with-custom-android-adapter-for-multiple-views-in | |
>>Creating ViewHolders for ListViews with different item layouts | |
class Adapter extends BaseAdapter { | |
... | |
public static final int SIMPLE_MODE = 110; | |
public static final int HEADER_MODE = 111; | |
public static final int VIEW_TYPE_0_LOCAL_HEADER = 0; | |
public static final int VIEW_TYPE_1_LOCAL_ITEMS = 1; | |
public static final int VIEW_TYPE_2_SEARCH_HEADER = 2; | |
public static final int VIEW_TYPE_3_FOUND_ITEMS = 3; | |
public static final int HOW_MANY_VIEW_TYPES_IN_HEADER_MODE = 4; | |
... | |
@Override | |
public int getViewTypeCount() { | |
android.util.Log.e(TAG, "int getViewTypeCount(); workingMode=" + workingMode); | |
if (workingMode == HEADER_MODE) | |
return HOW_MANY_VIEW_TYPES_IN_HEADER_MODE; | |
else if (workingMode == SIMPLE_MODE) | |
return super.getViewTypeCount(); | |
else | |
throw new RuntimeException("int getItemViewType(int) unknown workingMode"); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
android.util.Log.e(TAG, "int getItemViewType(int); position=" + position); | |
if (workingMode == HEADER_MODE) { | |
if (position == 0) | |
return VIEW_TYPE_0_LOCAL_HEADER; | |
else if (position == chatUsers.size() + 1) | |
return VIEW_TYPE_2_SEARCH_HEADER; | |
if (position < chatUsers.size() + 1) { | |
return VIEW_TYPE_1_LOCAL_ITEMS; | |
} else { | |
return VIEW_TYPE_3_FOUND_ITEMS; | |
} | |
} else if (workingMode == SIMPLE_MODE) { | |
return VIEW_TYPE_1_LOCAL_ITEMS; | |
} else { | |
throw new RuntimeException("int getItemViewType(int) unknown workingMode"); | |
} | |
} | |
@Override | |
public int getCount() { | |
if (workingMode == HEADER_MODE) { | |
android.util.Log.e(TAG, "int getCount(); return=" + (2 + chatUsers.size() + searchContacts.size())); | |
return 2 + chatUsers.size() + searchContacts.size(); | |
} else if (workingMode == SIMPLE_MODE) { | |
android.util.Log.e(TAG, "int getCount(); return=" + (chatUsers.size())); | |
return chatUsers.size(); | |
} else { | |
throw new RuntimeException("int getCount() unknown workingMode"); | |
} | |
} | |
@Override | |
public ChatUser getItem(int position) { | |
android.util.Log.e(TAG, "ChatUser getCount(int); position=" + position); | |
if (workingMode == HEADER_MODE) { | |
int type = getItemViewType(position); | |
if (type == VIEW_TYPE_1_LOCAL_ITEMS) { | |
android.util.Log.e(TAG, "ChatUser getCount(int); type=" + VIEW_TYPE_1_LOCAL_ITEMS); | |
return chatUsers.get(position-1); | |
} else if (type == VIEW_TYPE_3_FOUND_ITEMS) { | |
android.util.Log.e(TAG, "ChatUser getCount(int); type=" + VIEW_TYPE_3_FOUND_ITEMS); | |
return searchContacts.get(position - (chatUsers.size() + 2)); | |
} else { | |
android.util.Log.e(TAG, "ChatUser getCount(int); type=" + type); | |
return null; | |
} | |
} else if (workingMode == SIMPLE_MODE) { | |
return chatUsers.get(position); | |
}else { | |
throw new RuntimeException("ChatUser getItemViewType(int) unknown workingMode"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment