Android listview中的复选框有时不起作用

Android listview中的复选框有时不起作用,android,Android,我有一个对话框,其中包含带有复选框的listview和一个按钮,如果至少选中一个复选框,该按钮将变为可见。单击按钮可从listview中删除选中的行。但在这个对话框中,列表中的复选框有一种奇怪的行为。例如,如果列表包含四行,并且我选中其中一行,则无法再检查更多项。如果我在列表中添加了更多项目,那么我可以选中多个项目,但是如果我删除了一些项目,剩余的项目将不可选中 另外,有没有办法解决这个问题 更新1:我发现复选框不会变得绝对不活动,如果尝试单击一个复选框几次,它就会被选中。看起来复选框周围被识别

我有一个对话框,其中包含带有复选框的listview和一个按钮,如果至少选中一个复选框,该按钮将变为可见。单击按钮可从listview中删除选中的行。但在这个对话框中,列表中的复选框有一种奇怪的行为。例如,如果列表包含四行,并且我选中其中一行,则无法再检查更多项。如果我在列表中添加了更多项目,那么我可以选中多个项目,但是如果我删除了一些项目,剩余的项目将不可选中

另外,有没有办法解决这个问题

更新1:我发现复选框不会变得绝对不活动,如果尝试单击一个复选框几次,它就会被选中。看起来复选框周围被识别为onClick区域的区域设置得太少

UPDATE2:我还发现,如果我在对话框初始化代码中注释这一行

getWindow().setLayout(LinearLayout.LayoutParams.FILL_父项,LinearLayout.LayoutParams.WRAP_内容)

然后,在对话框实例化之后,列表中的所有复选框都不可选中

对话框布局

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:minHeight="200dip"
  android:orientation="vertical"
  android:background="@android:color/white"
>
  <ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:divider="@color/list_divider"
    android:dividerHeight="2px"
    android:focusable="false"
    android:clickable="false"
    android:choiceMode="multipleChoice"
    />
  <LinearLayout
    android:id="@+id/buttons_pane"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
  >
    <Button
      android:id="@+id/misc_list_remove_btn"
      android:layout_width="fill_parent"
      android:layout_height="48dip"
      android:text="@string/remove"
    />
  </LinearLayout>
</LinearLayout>
和列表中的项目

@Override
protected View instantiateView (Object ... params)
{
  String title = (String)params[0];
  CompoundButton.OnCheckedChangeListener checkedChangeListener =
    (CompoundButton.OnCheckedChangeListener)params[1];

  LayoutInflater inflater = Utils.getInflater (getContext ());
  View view = inflater.inflate (R.layout.misc_selectable_list_item, null);
  view.setId (title.hashCode ());
  CheckBox chk = (CheckBox)view.findViewById (R.id.misc_selectable_list_item_chk);
  chk.setText (title);
  chk.setOnCheckedChangeListener (checkedChangeListener);
  return view;
}

出现此问题是因为我在列表适配器的项中缓存了新创建的视图。如果列表项不包含诸如复选框或按钮之类的控件,它可以正常工作(至少没有可见的问题),但在某些情况下可能会成为问题的原因

避免列表项视图重新实例化的正确方法是使用在android.widget.Adapter中声明的方法的convertView参数


案例结束。

出现问题是因为我在列表适配器的项中缓存了新创建的视图。如果列表项不包含诸如复选框或按钮之类的控件,它可以正常工作(至少没有可见的问题),但在某些情况下可能会成为问题的原因

避免列表项视图重新实例化的正确方法是使用在android.widget.Adapter中声明的方法的convertView参数


案件结案。

我认为问题出在你的ClickListener上。尝试禁用它(即注释掉行
chk.setOnChangeListener()
)。如果我删除了click listener并注释掉了show/hide按钮代码,那么我可以选择多个项目,但在我从列表中删除了选中的项目后,我无法再选择了。签出这一行,我认为问题出在你的ClickListener中。尝试禁用它(即,注释掉行
chk.setOnChangeListener()
。如果我删除了click listener并注释掉了show/hide按钮代码,那么我可以选择多个项目,但在我从列表中删除了选中的项目后,我就不能再选择了。请同时签出此项。)
public ShowSelectableListDialog (Activity context, int titleId)
{
    super (context);
    setContentView (R.layout.misc_selectable_list_dlg);
    setTitle (titleId);
    getWindow ().setLayout (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    adapter_ = setupListAdapter (getInitialValues (), context);
    ListView listView = (ListView)findViewById (R.id.list_view);
    listView.setAdapter (adapter_);
    listView.setItemsCanFocus (false);
    Button removeButton = (Button)findViewById (R.id.misc_list_remove_btn);
    removeButton.setOnClickListener (this);
  }
@Override
protected View instantiateView (Object ... params)
{
  String title = (String)params[0];
  CompoundButton.OnCheckedChangeListener checkedChangeListener =
    (CompoundButton.OnCheckedChangeListener)params[1];

  LayoutInflater inflater = Utils.getInflater (getContext ());
  View view = inflater.inflate (R.layout.misc_selectable_list_item, null);
  view.setId (title.hashCode ());
  CheckBox chk = (CheckBox)view.findViewById (R.id.misc_selectable_list_item_chk);
  chk.setText (title);
  chk.setOnCheckedChangeListener (checkedChangeListener);
  return view;
}