复选框列表视图问题

时间:2013-01-17 05:02:14

标签: android android-listview listadapter

我有一个填充了复选框的列表视图。每个框的文本都使用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为null。

3 个答案:

答案 0 :(得分:1)

您可以通过在代码中添加else部分来避免此问题,如下所示

if(cbTags.equals(tags[j]))
{
cb.setChecked(true);
}else{
cb.setChecked(false);
}

:)

答案 1 :(得分:0)

这不是您通常使用ListView的方式。通常,您只需在onCreate中设置适配器,以及初始值列表。然后在Adapter.getView中,您可以将复选框的值设置为视图应该是什么。像这样调用adapter.getView是一个坏主意 - 列表视图使用该函数来创建和初始化当前屏幕上的视图,这可能会导致问题。

答案 2 :(得分:0)

我找到了解决问题的方法,可能有更好的方法,如果有的话,我很乐意听到。我在this的答案中使用了我找到的内容。当我在markAllTags()内拨打onStart()时,我用它包围了它。

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      public void run() {
          markAllTags();
      }
    }, 100);

现在我的markAllTags()函数看起来像这样:

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 = mListView.getChildAt(i);
        //View v = this.getListAdapter().getView(i, null, null);
        //Log.w("NME", "I = " + this.getListAdapter().getView(i, null, null).findViewById(R.id.checkBox1));
        Log.w("NME", "checking to see if V != null");
        if(v != null)
        {
            Log.w("NME", "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);
                }
            }//end for
        }//end if null

    }//end for
}//end

正如我在上一次编辑中所提到的,我改变了设置View v的位置,而不是它应该大致相同。

相关问题