ListView的getChildAt返回null可见儿童 [英] ListView getChildAt returning null for visible children

查看:92
本文介绍了ListView的getChildAt返回null可见儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个ListView /的getChildAt方法,一些奇怪的行为。

I'm getting some strange behavior from a listview/the getChildAt method.

我有一个HashSet已在数据库中更改图标,iconsToUpdate。我想遍历可见的行,看看是否有自己的图标,需要进行更新,以反映新的图标。我并不需要考虑当前未测试的图标,因为它们呈现时将被正确绘制。

I have a HashSet, iconsToUpdate, of icons that have been changed in the database. I want to iterate over the visible rows to see if any of their icons need to be updated to reflect the new icons. I don't need to test the icons not currently in view as they will be drawn properly when rendered.

我的问题是getChildAt将返回null,则好像不应该。我知道getChildAt只能返回看法是当前可见的,但它返回null对于一些可见行。

My problem is that getChildAt is returning null when it seems like it shouldn't. I know that getChildAt can only return views that are currently visible, but it is returning null for some of the visible rows.

下面是我的code,它遍历可见行:

Here is my code that iterates over the visible rows:

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

下面是日志对应于这个循环的运行:

Here is the log corresponding to a run of this loop:

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正在呈现正常。如何显示他们,如果他们是空?

It should be noted that the first and last visible indexes are being correctly reported, as I am viewing rows 3-8 when I run this. Rows 6, 7, 8 are being rendered properly. How are they being displayed if they are null?

另外,我不知道这是重要的,但第5行是最后一个可见行,当我在列表视图的顶部。

Also, I do not know if this is important, but row 5 is the last visible row when I am at the top of the listview.

任何信息,为什么这些行正在返回空值将大大AP preciated。

Any info as to why these rows are being returned as null would be greatly appreciated.

谢谢!

推荐答案

listView.getChildAt(我)的作品,其中0是第一个可见行和(N-1)是最后一个可见行(其中n是多少你看可见视图)。

listView.getChildAt(i) works where 0 is the very first visible row and (n-1) is the last visible row (where n is the number of visible views you see).

在得到最后一个/第一个可见的回报DataAdapter的你的位置。所以你既然你开始第3位,与什么样子6可见的意见,这时候得到的职位6-8你空。

The get last/first visible return the position in the dataAdapter you have. So you since you start at position 3, with what looks like 6 visible views, that's when get for positions 6-8 you get null.

在你的榜样getChildAt(0)将返回位置3.我通常做的是什么存储在我的意见的立场,所以我可以在我的DataAdapter后,如果我需要值查找。

In your example getChildAt(0) would return position 3. What I usually do is store the position on my views so I can lookup on my dataAdapter later if I need values.

我觉得你的for循环应该是这样的:

I think your for loop should look like this:

for (int i = 0; i <= f_listView.getLastVisiblePosition() - f_listView.getFirstVisiblePosition(); i++) 

希望这有助于。

Hope this helps.

这篇关于ListView的getChildAt返回null可见儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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