【发布时间】:2011-07-20 18:26:22
【问题描述】:
我从 listview/getChildAt 方法中得到了一些奇怪的行为。
我有一个 HashSet,iconsToUpdate,是数据库中已更改的图标。我想遍历可见行以查看是否需要更新它们的任何图标以反映新图标。我不需要测试当前不在视图中的图标,因为它们会在渲染时正确绘制。
我的问题是 getChildAt 似乎不应该返回 null。我知道 getChildAt 只能返回当前可见的视图,但它为某些可见行返回 null。
这是我在可见行上迭代的代码:
Logger.debug("First visible index: " + f_listView.getFirstVisiblePosition());
Logger.debug("Last visible index: " + f_listView.getLastVisiblePosition());
for (int i = f_listView.getFirstVisiblePosition(); i <= f_listView.getLastVisiblePosition(); i++) {
String tag = "asdf"; // Remove when bug is fixed.
if (f_listView == null) {
Logger.debug("f_listView is null");
} else if (f_listView.getChildAt(i) == null) {
Logger.debug("Child at index " + i + " is null");
} else {
tag = (String) f_listView.getChildAt(i).getTag();
Logger.debug("Successful at index " + i + ", tag is: " + tag);
}
if (iconsToUpdate.contains(tag)) {
setIcon(i, f_aim.getInHouseIcon(tag));
}
}
这是与此循环运行相对应的日志:
D/...: First visible index: 3
D/...: Last visible index: 8
D/...: Successful at index 3, tag is: ...
D/...: Successful at index 4, tag is: ...
D/...: Successful at index 5, tag is: ...
D/...: Child at index 6 is null
D/...: Child at index 7 is null
D/...: Child at index 8 is null
应该注意的是,第一个和最后一个可见索引被正确报告,因为我在运行它时正在查看第 3-8 行。第 6、7、8 行正在正确渲染。如果它们为空,它们如何显示?
另外,我不知道这是否重要,但是当我在列表视图的顶部时,第 5 行是最后一个可见行。
任何关于为什么这些行被返回为 null 的信息将不胜感激。
谢谢!
【问题讨论】: