最近在做项目时,需求上需要ListView 有两个不同的布局,由于自己定义getItemViewType 的返回值,出现一个异常,
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6647)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4998)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4595)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:769)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:582)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:551)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:755)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5298)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
让我绞尽脑汁, 后来看了下Adapter源码发现以下代码:
/**
* Get the type of View that will be created by {@link #getView} for the specified item.
*
* @param position The position of the item within the adapter's data set whose view type we
* want.
* @return An integer representing the type of View. Two views should share the same type if one
* can be converted to the other in {@link #getView}. Note: Integers must be in the
* <span style="background-color: rgb(51, 255, 51);"> range 0 to {@link #getViewTypeCount} - 1.</span> {@link #IGNORE_ITEM_VIEW_TYPE} can<pre name="code" class="java">
int getItemViewType(int position);
意思是 getItemVIewType这个方法返回的整型范围: 0 to (getViewTypeCount() -1)
getItemViewType 的返回值不能随便定义:
如下是我项目中的定义:
@Override
public int getItemViewType(int position) {
AppLog.Log("firstData_view_type:" + lists.get(position).getCategory());
int category = lists.get(position).getCategory();
if (category == 1) {
return 0;
}
return 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
写这篇文章,一个是为了让自己不再犯错, 二是给广大安卓开发者有遇到如似问题,可以快速解决。