用于 ListView 中多个视图的自定义 Android 适配器的 ArrayIndexOutOfBoundsException
我正在尝试为 ListView 创建自定义适配器,因为列表中的每个项目都可以有不同的视图(链接、切换或单选组),但是当我尝试运行使用 ListView 的活动时,我收到错误并且应用程序停止。该应用程序针对Android 1.6平台。
代码:
public class MenuListAdapter extends BaseAdapter {
private static final String LOG_KEY = MenuListAdapter.class.getSimpleName();
protected List<MenuItem> list;
protected Context ctx;
protected LayoutInflater inflater;
public MenuListAdapter(Context context, List<MenuItem> objects) {
this.list = objects;
this.ctx = context;
this.inflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(LOG_KEY, "Position: " + position + "; convertView = " + convertView + "; parent=" + parent);
MenuItem item = list.get(position);
Log.i(LOG_KEY, "Item=" + item );
if (convertView == null) {
convertView = this.inflater.inflate(item.getLayout(), null);
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public MenuItem getItem(int position) {
return this.list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
Log.i(LOG_KEY, "getItemViewType: " + this.list.get(position).getLayout());
return this.list.get(position).getLayout();
}
@Override
public int getViewTypeCount() {
Log.i(LOG_KEY, "getViewTypeCount: " + this.list.size());
return this.list.size();
}
}
我收到的错误:
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3523)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1158)
at android.widget.ListView.onMeasure(ListView.java:1060)
at android.view.View.measure(View.java:7703)
我确实知道应用程序正在从 getView
返回,并且一切似乎都按顺序进行。
任何关于可能导致此问题的想法将不胜感激。
谢谢,
-丹
I am attempting to create a custom Adapter for my ListView since each item in the list can have a different view (a link, toggle, or radio group), but when I try to run the Activity that uses the ListView I receive an error and the app stops. The application is targeted for the Android 1.6 platform.
The code:
public class MenuListAdapter extends BaseAdapter {
private static final String LOG_KEY = MenuListAdapter.class.getSimpleName();
protected List<MenuItem> list;
protected Context ctx;
protected LayoutInflater inflater;
public MenuListAdapter(Context context, List<MenuItem> objects) {
this.list = objects;
this.ctx = context;
this.inflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(LOG_KEY, "Position: " + position + "; convertView = " + convertView + "; parent=" + parent);
MenuItem item = list.get(position);
Log.i(LOG_KEY, "Item=" + item );
if (convertView == null) {
convertView = this.inflater.inflate(item.getLayout(), null);
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public MenuItem getItem(int position) {
return this.list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
Log.i(LOG_KEY, "getItemViewType: " + this.list.get(position).getLayout());
return this.list.get(position).getLayout();
}
@Override
public int getViewTypeCount() {
Log.i(LOG_KEY, "getViewTypeCount: " + this.list.size());
return this.list.size();
}
}
The error I receive:
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3523)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1158)
at android.widget.ListView.onMeasure(ListView.java:1060)
at android.view.View.measure(View.java:7703)
I do know that the application is returning from getView
and everything seems in order.
Any ideas on what could be causing this would be appreciated.
Thanks,
-Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您从
getItemViewType()
返回的项目视图类型是
>= getViewTypeCount()
。The item view type you are returning from
getItemViewType()
is>= getViewTypeCount()
.接受的答案是正确的。这就是我正在做的事情来避免这个问题:
The accepted answer is correct. This is what I am doing to avoid the problem:
当 getItemType 值错误时会出现问题。
该值应该是一个整数,并且应该在 0 到 getViewTypeCount()-1 之间。
Issue comes when getItemType value is wrong.
This value should be an integer and should be from 0 to getViewTypeCount()-1.
原因很可能是因为 getItemViewType 方法返回了错误的值!
列表视图中的每一行都是一个视图。当滚动 getItemViewType 达到超过 View 的计数时。
该怎么办?如何避免问题?
首先确定首先显示的列表视图中的视图(行)。滚动时使用模块方程
在此示例中,有十个视图(10 行)。
重要
一些用户在 stackoverflow 上的另一个问题上提到了该方法
在 getView 覆盖上 执行方法。
The reason, is most likely because the getItemViewType method is returning the wrong values!
Each row in listview is a one View. While scrooling getItemViewType reach more than View's count.
What to do? How to avoid issue?
First determine view(row) in your listview that is shown first.While Scrolling use moduler equation
In this example there is ten view(10 row).
Important
Some users mentioned on another question on stackoverflow that method
on getView override method.