过滤后的Android ListView数组索引超出界限 [英] Android ListView array index out of bounds after filter

查看:328
本文介绍了过滤后的Android ListView数组索引超出界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是专家的问题。



我使用 positon 超出范围来调用 getView() ListView 数据列表。

当我使用Adapter过滤器时,会发生这种情况。过滤器 publishResults()方法使用小于原始列表的过滤列表来填充数据。

当新过滤器列表比之前的过滤列表短。
我改变了代码 getView()返回一个虚拟的 convertView 出界时,只是看有多少这样的调用

这些是相关的代码和我登录的 log 消息:

  @Override 
public View getView(int position,View convertView,ViewGroup parent){
//这里没有日志来保持ListView的性能
Log.d(TAG,+ getView(position =+ position +));
ViewHolder持有者;

if(position> = mData.size()){
//这段代码允许查看有多少坏的电话我得到
Log.w(TAG,position越界!);
convertView = mInflater.inflate(mLayout,parent,false);
返回convertView;
}

。 。 。 //普通的getView代码

return convertView;



$ b $ p
$ b

在过滤器中(代码复制为 ArrayAdapter 源代码)

  @Override 
protected void publishResults(CharSequence约束,FilterResults结果) {
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个项目(我标记为 *** 出界限调用) :

pre $ 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已释放378K,5%免费13577K / 14215K,暂停0ms + 1ms
02-26 05:31:56.153:D / LogScreenAdapter (22857): - performFiltering()
02-26 05:31:56.153:D / LogScreenAdapter(22857):+ publishResults(约束: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 (位置= 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已释放383K,5%免费13577K / 14215K,暂停0ms + 0ms
02-26 05:32:23.924:D / dalvikvm 22857):GC_CONCURRENT释放388K,5%免费13573K / 14215K,暂停0ms + 1ms
02-26 05:32:23.974:D / LogScreenAdapter(22857): - performFiltering()
02-26 05 :32:23.983:D / LogScreenAdapter(22857):+ publishResults(约束:logs,results.count:3)
02-26 05:32:23.983:D / LogScreenAdapter(22857) b $ b 02-26 05:32:23.983:D / LogScreenAdapter(22857):+ getView(position = 0)
02-26 05:32:24.074:D / LogScreenAdapter(22857):+ getView = 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)边界!
02-26 05:32:24.233:D / LogScreenAdapter(22857):+ getView(position = 4)
*** 02-26 05:32:24.263:W / LogScreenAdapter(22857):位置超出界限!
02-26 05:32:24.284:D / LogScreenAdapter(22857):+ getView(position = 5)
*** 02-26 05:32:24.313:W / LogScreenAdapter(22857):位置超出界限!
02-26 05:32:24.333:D / LogScreenAdapter(22857):+ getView(position = 6)
*** 02-26 05:32:24.343:W / LogScreenAdapter(22857):位置超出界限!
02-26 05:32:24.353 D / LogScreenAdapter(22857):+ getView(position = 0)
02-26 05:32:24.373:D / LogScreenAdapter(22857):+ getView位置= 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):位置超出界限!
02-26 05:32:24.463:D / LogScreenAdapter(22857):+ getView(position = 5)
*** 02-26 05:32:24.475:W / LogScreenAdapter(22857):位置超出界限!
02-26 05:32:24.483:D / LogScreenAdapter(22857):+ getView(position = 6)
*** 02-26 05:32:24.503:W / LogScreenAdapter(22857):位置超出界限!
02-26 05:38:26.769:D / dalvikvm(22857):GC_CONCURRENT释放了316K,5%免费13640K / 14215K,暂停0ms + 1ms

您在这里看到的 publishResults()方法确实将mData从7个项目列表更改为更短列表中的3个项目,请参阅上面的代码,但 Adapter 不断获得7个项目清单的 getView()调用,即使它不在那里。

注意, notifyDataSetChanged()已经被新的数据赋值调用,所以 ListView 应该知道新列表。

解决方案


$ b> public int getCount()自定义列表视图适配器的方法?

您应该像 mData!=空值? mData.size():0

由于可能您正在返回列表的大小超过了数据显示在列表中


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

I think this is a question for experts.

I get calls to getView() with positon out of bounds from the ListView data list.
This happens when I use the Adapter filter. The filter publishResults() method populates the data with a filtered list which is smaller than the original list.
The error seems to occur when the new filtered list is shorter than the previous filtered list. I changed the code of getView() to return a dummy convertView when out of bounds, just to see how many such calls are issued.

These are the relevant code and the log messages I logged:

    @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;
    }

In the filter (code copied as is from ArrayAdapter source code)

        @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()");
        }

The log file shows that after a filter with 7 results, comes a fitler with 3 results, but getView keeps getting calls for 7 items (I marked with *** the out of bounds calls):

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

What you are seeing here, the publishResults() method did change mData from a list of 7 items to a shorter list of 3 items, see the code above, but the Adapter keeps getting getView() calls for the 7 items list, even that it is not longer there.
Note that notifyDataSetChanged() has been called with the new data assignment, so the ListView should be aware of the new list.

解决方案

what are you returning in public int getCount() method of custom list view adapter?

You should return like mData != null? mData.size() : 0,

Position out of bond is getting due to may be you are returning size of list more than data to show in the list

getCount() method of custom list adapter specifies size of list view so it should be the size of data you are passing in the list

这篇关于过滤后的Android ListView数组索引超出界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆