Android ListView数组索引过滤后越界

2024-05-12

我认为这是专家的问题。

我接到电话getView() with positon 出界来自ListView数据列表。
当我使用适配器过滤器时会发生这种情况。过滤器publishResults()方法使用小于原始列表的过滤列表填充数据。
当新的过滤列表时似乎会发生错误比之前的短过滤列表。 我改变了代码getView()返回一个假人转换视图当出界时,只需查看发出了多少次此类调用。

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

    @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不断得到getView()要求提供 7 项清单,即使该清单已不存在。
注意notifyDataSetChanged()已使用新的数据分配进行调用,因此ListView应该了解新的清单。


你要返回什么public int getCount()自定义列表视图适配器的方法?

你应该像这样返回mData != null? mData.size() : 0,

由于您返回的列表大小可能超过列表中显示的数据,因此头寸超出保证金

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android ListView数组索引过滤后越界 的相关文章

随机推荐