gpt4 book ai didi

过滤后Android ListView数组索引越界

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:29 24 4
gpt4 key购买 nike

我认为这是专家的问题。

我从 ListView 数据列表中收到 getView() 调用,positon 越界。< br/>当我使用 Adapter 过滤器时会发生这种情况。过滤器 publishResults() 方法使用小于原始列表的过滤列表填充数据。
当新的过滤列表比以前的过滤列表短时,似乎会发生错误。我更改了 getView() 的代码以在越界时返回虚拟 convertView,只是为了查看发出了多少次此类调用。

这些是相关代码和我记录的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 不断收到对 7 项列表的 getView() 调用,即使它不再存在。
请注意,notifyDataSetChanged() 已通过新数据分配调用,因此 ListView 应该知道新列表。

最佳答案

您在自定义 ListView 适配器的 public int getCount() 方法中返回什么?

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

债券到期的头寸可能是因为您返回的列表大小多于列表中显示的数据

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

关于过滤后Android ListView数组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com