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

本文是小编为大家收集整理的关于getChildCount()在ListView上返回0的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要检查ListView上的所有元素,以将标签设置为其中一个. 我无法编辑数据库或适配器,我只想滚动ListView来执行检查并在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("");
        }       
    }
}

我该怎么办?我需要等待一些东西?

p.s. shoply listview不为空.

推荐答案

似乎是一个令人讨厌的黑客攻击.但好吧...

是,只要没有向用户显示列表,您的列表就不会有孩子. 但你必须了解getChildCount将返回可见列表项的数量(所以可能大约10个视图),它们的位置永远不会与适配器中的实际项目位置相关.

如果您真的需要与如此低级的视图通信,您可以尝试将滚动侦听器附加到列表:

@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
    }
}

本文地址:https://www.itbaoku.cn/post/102374.html

问题描述

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 the ListView is not empty.

推荐答案

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

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