【问题标题】:Why ListView.getChildCount()==0 (always)?为什么 ListView.getChildCount()==0 (总是)?
【发布时间】:2011-12-28 01:46:16
【问题描述】:

我想在活动创建时为 ListView 的某些项目设置 SpannableString。
我是这样写的:

    doListlist=(ListView)findViewById(R.id.dolist_List);
    listsDbAdapter=new yueDataBaseAdapter(this);
    listsDbAdapter.open();
    UpdataAdpter();

    for(int i = 0; i < doListlist.getChildCount(); i++) {  
        if (listsDbAdapter.fetchData(tableName, i+1).getString(7).length() == 4) {

          View view = doListlist.getChildAt(i);  

          String drawText=listsDbAdapter.fetchData(tableName,i+1).getString(1);

          TextView txtInfo =(TextView)view.findViewById(R.id.doList_itemname);

          SpannableString ss = new SpannableString(drawText); 
          ss.setSpan(new StrikethroughSpan(), 0, drawText.length(),  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          txtInfo.setText(ss);
        }
    }

    public void UpdataAdpter(){
    Cursor cur= listsDbAdapter.fetchAllData(tableName);
    if(cur!=null&& cur.getCount()>=0){
        adapter =new SimpleCursorAdapter(this,
                R.layout.dolist_item,
                cur,
                new String[]{yueDataBaseAdapter.KEY_NAME,yueDataBaseAdapter.KEY_PRICESTR,yueDataBaseAdapter.KEY_NUMSTR},
                new int[]{R.id.doList_itemname,R.id.doList_itemPrice,R.id.doList_itemNum}
        );

        doListlist.setAdapter(adapter);
    }
}

但我发现 doListlist.getChildCount() 总是 = 0。
问题在哪里?或者还有其他更好的方法吗?

【问题讨论】:

  • 您的列表未正确填充。显示您正在使用的代码以填充它。
  • 尝试使用 doListList.getAdapter().getCount()。

标签: android listview spannablestring


【解决方案1】:

因为您还没有设置列表的适配器。做

doListlit.setAdapter(listsDbAdapter);

首先,在调用getChildCount之前。

【讨论】:

  • 对不起,我错过了。你能检查 cursor.getCount() 的值吗?
  • 我是这样写的:for(int i = 0; i
【解决方案2】:

看看这个:

getChildCount() returns 0 on ListView

它对我有用。 我希望它会有所帮助

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2020-05-25
    相关资源
    最近更新 更多