ArrayIndexOutOfBoundsException 与 ListView 中多个视图的自定义 Android 适配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2596547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:29:38  来源:igfitidea点击:

ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView

androidexceptionlistviewadapter

提问by Dan

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.

我正在尝试为我的 ListView 创建自定义适配器,因为列表中的每个项目都可以有不同的视图(链接、切换或单选组),但是当我尝试运行使用 ListView 的活动时,我收到一个错误和应用程序停止。该应用程序面向 Android 1.6 平台。

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 getViewand everything seems in order.

我知道应用程序正在返回getView,一切似乎都井然有序。

Any ideas on what could be causing this would be appreciated.

任何关于可能导致这种情况的想法将不胜感激。

Thanks,

谢谢,

-Dan

-担

回答by Romain Guy

The item view type you are returning from

您正在返回的项目视图类型

getItemViewType()is >= getViewTypeCount().

getItemViewType()>= getViewTypeCount()

回答by mtbomb

The accepted answer is correct. This is what I am doing to avoid the problem:

接受的答案是正确的。这是我正在做的事情来避免这个问题:

public enum FoodRowType {
    ONLY_ELEM,
    FIRST_ELEM,
    MID_ELEM,
    LAST_ELEM
}

@Override
public int getViewTypeCount() {
    return FoodRowType.values().length;
}

@Override
public int getItemViewType(int position) {
    return rows.get(position).getViewType();  //returns one of the above types
}

回答by sunil jain

Issue comes when getItemType value is wrong. This value should be an integer and should be from 0 to getViewTypeCount()-1.

当 getItemType 值错误时会出现问题。该值应该是一个整数,并且应该是从 0 到 getViewTypeCount()-1。

回答by Beyaz

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.

原因很可能是因为 getItemViewType 方法返回了错误的值!列表视图中的每一行都是一个视图。滚动 getItemViewType 时达到的数量超过了 View 的数量。

What to do? How to avoid issue?

该怎么办?如何避免问题?

First determine view(row) in your listview that is shown first.While Scrolling use moduler equation

首先确定首先显示的列表视图中的视图(行)。虽然滚动使用模块方程

    @Override
    public int getItemViewType(int position) {
        return choseType(position);//function to use modular equation
    }
    @Override
    public int getViewTypeCount() {
        return 10;
    }

In this example there is ten view(10 row).

在这个例子中有十个视图(10 行)。

private  int choseType(int position){
    if(position%10==0)
        return 0;
    else if(position%10==1)
        return 1;
    else if(position%10==2)
        return 2;
    else if(position%10==3)
        return 3;
    else if(position%10==4)
        return 4;
    else if(position%10==5)
        return 5;
    else if(position%10==6)
        return 6;
    else if(position%10==7)
        return 7;
    else if(position%10==8)
        return 8;
    else
        return 9;


}

Important

重要的

Some users mentioned on another question on stackoverflow that method

一些用户在关于 stackoverflow 的另一个问题中提到了该方法

public int getViewTypeCount() and public int getItemViewType(int position) fix like Tooglebutton automaticly enable state check true on scrooling..that is big wrong.If you dont want automatic enbale on scrool just do

public int getViewTypeCount() 和 public int getItemViewType(int position) 修复像 Tooglebutton 自动启用状态检查 true 在滚动时..这是一个很大的错误。如果你不想在滚动时自动启用就做

toogleButton.setChecked(false);

on getView override method.

在 getView 覆盖方法上。