Android ListView在过滤后出现数组越界异常

15
我认为这是一个专家级问题。
当我使用适配器过滤器时,我会收到从数据列表中超出范围的positon的getView()调用。发生这种情况时,过滤器publishResults()方法会用较小的过滤列表填充数据。
当新过滤列表比以前的过滤列表短时,错误似乎会发生。 当越界时,我改变了getView()的代码,返回一个虚拟convertView,只为看到有多少这样的调用。
这些是相关的代码和我记录的日志消息:
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // No logs here to keep ListView performance good
        Log.d(TAG, "+ getView( position=" + position + ")");
        ViewHolder holder;

        if( position >= mData.size() ) {
            // This code allows to see how many bad calls I get
            Log.w(TAG, "position out of bounds!");
            convertView = mInflater.inflate(mLayout, parent, false);
            return convertView;
        }

        . . . // Normal getView code

        return convertView;
    }

在过滤器中(代码从 ArrayAdapter 源代码中直接复制)
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.pe(TAG, "+ publishResults(constraint:" + constraint + ", results.count:" + results.count + ")");
            //noinspection unchecked
            mData = (ArrayList<String>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
            Log.px(TAG, "- publishResults()");
        }

日志文件显示,在筛选出7个结果后,又进行了一次筛选,只剩下3个结果,但是getView方法仍然会被调用7次(我用***标记了越界的调用):

02-26 05:31:55.986: D/ViewerActivity(22857): + onQueryTextChange(newText:log)
02-26 05:31:55.986: D/ViewerActivity(22857): - onQueryTextChange()
02-26 05:31:56.029: D/LogScreenAdapter(22857): + performFiltering(prefix:log)
02-26 05:31:56.113: D/dalvikvm(22857): GC_CONCURRENT freed 378K, 5% free 13577K/14215K, paused 0ms+1ms
02-26 05:31:56.153: D/LogScreenAdapter(22857): - performFiltering()
02-26 05:31:56.153: D/LogScreenAdapter(22857): + publishResults(constraint:log, results.count:7)
02-26 05:31:56.167: D/LogScreenAdapter(22857): - publishResults()
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=1)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=2)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=3)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=4)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=5)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=6)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=1)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=2)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=3)
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView( position=4)
02-26 05:31:56.493: D/LogScreenAdapter(22857): + getView( position=5)
02-26 05:31:56.503: D/LogScreenAdapter(22857): + getView( position=6)
02-26 05:32:23.793: D/ViewerActivity(22857): + onQueryTextChange(newText:logs)
02-26 05:32:23.793: D/ViewerActivity(22857): - onQueryTextChange()
02-26 05:32:23.813: D/LogScreenAdapter(22857): + performFiltering(prefix:logs)
02-26 05:32:23.854: D/dalvikvm(22857): GC_CONCURRENT freed 383K, 5% free 13577K/14215K, paused 0ms+0ms
02-26 05:32:23.924: D/dalvikvm(22857): GC_CONCURRENT freed 388K, 5% free 13573K/14215K, paused 0ms+1ms
02-26 05:32:23.974: D/LogScreenAdapter(22857): - performFiltering()
02-26 05:32:23.983: D/LogScreenAdapter(22857): + publishResults(constraint:logs, results.count:3)
02-26 05:32:23.983: D/LogScreenAdapter(22857): - publishResults()
02-26 05:32:23.983: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:32:24.074: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:32:24.093: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:32:24.113: D/LogScreenAdapter(22857): + getView( position=1)
02-26 05:32:24.155: D/LogScreenAdapter(22857): + getView( position=2)
02-26 05:32:24.164: D/LogScreenAdapter(22857): + getView( position=3)
*** 02-26 05:32:24.193: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.233: D/LogScreenAdapter(22857): + getView( position=4)
*** 02-26 05:32:24.263: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.284: D/LogScreenAdapter(22857): + getView( position=5)
*** 02-26 05:32:24.313: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.333: D/LogScreenAdapter(22857): + getView( position=6)
*** 02-26 05:32:24.343: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.353: D/LogScreenAdapter(22857): + getView( position=0)
02-26 05:32:24.373: D/LogScreenAdapter(22857): + getView( position=1)
02-26 05:32:24.383: D/LogScreenAdapter(22857): + getView( position=2)
02-26 05:32:24.403: D/LogScreenAdapter(22857): + getView( position=3)
*** 02-26 05:32:24.413: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.433: D/LogScreenAdapter(22857): + getView( position=4)
*** 02-26 05:32:24.443: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.463: D/LogScreenAdapter(22857): + getView( position=5)
*** 02-26 05:32:24.475: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:32:24.483: D/LogScreenAdapter(22857): + getView( position=6)
*** 02-26 05:32:24.503: W/LogScreenAdapter(22857): position out of bounds!
02-26 05:38:26.769: D/dalvikvm(22857): GC_CONCURRENT freed 316K, 5% free 13640K/14215K, paused 0ms+1ms

你所看到的是,publishResults()方法将mData从一个包含7个项目的列表更改为一个较短的3个项目的列表,请参见上面的代码,但是Adapter仍然会获取7个项目列表的getView()调用,即使它已经不再存在。
请注意,notifyDataSetChanged()已使用新数据分配进行了调用,因此ListView应该知道新列表。

2
getItemCount 函数是否被调用了?如果被调用的话,它返回了什么?如果你正在覆盖它,请发布代码。 - Gabe Sechan
2
getItemCount返回什么? - alex
我不覆盖getItemCount方法,我应该吗? - ilomambo
我认为这是专家问题。太滑稽了! - RobinHood
@RobinHood 这只意味着我不是专家。很高兴我能让你微笑。我改变了代码,到目前为止我没有再出现越界的情况。如果有人愿意发布答案,我会将其标记为已回答。 - ilomambo
@ilomambo,点赞支持你提出这个问题。对于一些人来说可能是初学者的问题,但我也曾遇到同样的问题,而这些答案解决了我的困惑。 - Mahm00d
3个回答

23

在自定义列表视图适配器的 public int getCount() 方法中,您应该返回像 mData != null? mData.size() : 0 这样的值。

由于您返回的列表大小超过了要显示在列表中的数据,因此可能会导致超出范围的位置错误。

自定义列表适配器的 getCount() 方法指定了列表视图的大小,因此它应该是您在列表中传递的数据的大小。


你救了我的一天!我完全忘记重写 getCount 方法了。 - Couitchy

21
似乎覆盖“getCount()”方法可以解决您的问题:
@Override
public int getCount() {
    return mData.size();
}

1
非常感谢......给这个加1。 - Noman
该语句存在,但是有相同的问题。 - Prasad

0

我做了这个,解决了我的问题

@Override
public int getCount() {

    if (isFiltered == true) {
        return myFilteredArray.size();
    }

    return myUnfilteredArray.size();
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接