getChildCount()上的ListView返回0 [英] getChildCount() returns 0 on ListView

查看:362
本文介绍了getChildCount()上的ListView返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一个ListView的所有元素的标签设置只对其中之一。 我不能编辑数据库或适配器,我只是想滚动的ListView执行检查并设置一个TextView的字符串。

I need to check all element on a ListView to set a label only to a one of those. I can't edit the database or the adapter, I just want to scroll the ListView to perform a check and set a string on a TextView.

@Override
protected void onResume(){
    super.onResume();
    ...
    cursor = getCursor();
    startManagingCursor(cursor);
    adapter = new SimpleCursorAdapter(this,R.layout.profile_item_layout,cursor,from,to);
    lst_profiles.setAdapter(adapter);
    SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(ProfilerActivity.this.getApplicationContext());
    long current = customSharedPreference.getLong(ProfileManager.CURRENT, -1);
    toast(lst_profiles.getChildCount()); //display 0
    if(current!=-1){
        for(int i=0;i<lst_profiles.getChildCount();i++){
            if(lst_profiles.getItemIdAtPosition(i)==current)
                ((TextView)lst_profiles.getChildAt(i).findViewById(R.id.activeLabel)).setText(getString(R.string.active));
            else
                ((TextView)lst_profiles.getChildAt(i).findViewById(R.id.activeLabel)).setText("");
        }       
    }
}

我该怎么办?我需要等待什么?

How can I do? I need to wait something?

P.S。 Obviusly ListView控件不为空。

P.S. Obviusly the ListView is not empty.

推荐答案

这似乎是一个讨厌的黑客攻击。不过还好......

That seems to be a nasty hack. But okay...

的事情是,你的名单将不会有孩子,只要列表不显示给用户。 但是您要明白, getChildCount 将返回可见的列表项的款额(大概在10次),其中绝不会涉及的位置到在适配器的实际零件的位置。

The thing is, that your list won't have children as long as the list is not displayed to the user. But you have to understand that getChildCount will return the amount of visible list items (so maybe about 10 views) and the position of them will never relate to the actual item position in the adapter.

如果您真的需要在这样一个低水平的意见沟通,你可以尝试将滚动监听到您的列表:

If you really need to communicate with the views on a such low level you could try to attach a scroll listener to your list:

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    for (int i = 0; i < visibleItemCount; i++) {
        View child = getChildAt(i);
        // i + firstVisibleItem == the actual item position in the adapter
    }
}

这篇关于getChildCount()上的ListView返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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