【问题标题】:Checkbox list view troubles复选框列表查看问题
【发布时间】: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


    【解决方案1】:

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

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

    :)

    【讨论】:

    • 感谢您的回答,但这并没有奏效。我编辑了我的问题,希望能让我的问题更清楚。
    【解决方案2】:

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

    【讨论】:

    • 好的,我相信我明白了。我应该如何检查某些复选框并不选中某些复选框?
    • 当给定行调用 getView 时,确定是否要选中该行中的复选框。如果这样做,请调用 setChecked(true)。如果没有,请调用 setChecked(false)。确保在这两种情况下都调用它,列表视图会为新滚动的行重用视图,因此复选框可能已经设置为选中,您需要明确取消选中它。
    • 感谢您的回答并没有完全解决我的问题,但它让我思考并找到了解决方案。我认为这也将帮助我避免未来的错误。我的解决方案可能不是最好的解决方案,但它现在会让我受益。不过,我必须等待 8 小时才能回答我自己的问题。
    【解决方案3】:

    我找到了解决问题的方法,可能有更好的方法,如果有的话,我很想听听。我使用了在this 的答案中找到的东西,当我在onStart() 中调用markAllTags() 时,我已经用它包围了它。

        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 的位置,但它应该大致相同。

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多