【发布时间】:2013-01-17 05:02:13
【问题描述】:
我有一个填充了复选框的列表视图。每个框上的文本都使用ListAdapter 设置。当所有内容加载完毕后,我需要默认选中一些复选框,而其中一些复选框不被选中。我可以在复选框上调用setChecked,但它不起作用。我相信这是因为在onCreate() 中,视图还不可见。所以我把它移到了onResume() 和onCreate(),到目前为止它仍然不起作用。似乎它们还没有加载并出现在屏幕上。
private void markAllTags()//this will put a check mark on the ones that are already a tag of that question
{
String tags[] = mUserDb.fetchTagById(mQid);//These are the tags that are already associated with this question
for(int i = 0; i < this.getListAdapter().getCount(); i++)
{
View v = this.getListAdapter().getView(i, null, null);
//Log.w("NME", "I = " + this.getListAdapter().getView(i, null, null).findViewById(R.id.checkBox1));
if(v != null)
{
CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1);
String cbTags = "" + cb.getText();
for(int j = 0; j < tags.length; j++)
{
//Log.w("NME", "cbTag = " + cbTags + " tags[j] = " + tags[j]);
if(cbTags.equals(tags[j]))
{
cb.setChecked(true);
}
}
}
}
}
当前正在从 onStart 调用。
如果我使用listview.getchildAt(),我会收到一个空值。
编辑:没有崩溃,所以 logcat 中没有真正出现。所以问题是,当满足最终的 if 语句时,它不会将其标记为已检查。
编辑2:所以我尝试了更多的东西。我将上面的功能链接到一个按钮,但它仍然不起作用。当我更改设置View v 的行时。对此View v = mListView.getChildAt(i); 当我按下按钮时它起作用了。但是,当活动开始时它仍然不起作用。现在的问题是 v 为空。
【问题讨论】:
标签: android android-listview listadapter