【问题标题】:Checking a checkbox in listview makes other random checkboxes checked too检查列表视图中的复选框也会检查其他随机复选框
【发布时间】:2012-06-25 13:36:35
【问题描述】:

每当我在列表视图中选中一个复选框时,其他随机复选框也会被选中。这可能是由于 listview 的项目回收。

我还尝试在某些地方建议的布局中设置android:focusable="false" to checkbox,但仍然onlistitemclick()在检查其复选框时仍未调用onlistitemclick()。当我点击其他地方被调用时,仍然会调用。

我想要的是只有用户选中的复选框应该保持选中状态,直到用户取消选中它们。

我在下面给出了完整的代码,可以直接运行。

活动代码-ProjActivity.java:

public class ProjActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    final CopyOfMyCustomAdapter a = new CopyOfMyCustomAdapter(this, packages);
    getListView().setAdapter(a);
}}

最后,自定义布局文件——testlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 

>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="CheckBox" 
    android:focusable="false"
    />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    android:focusable="false"
    />

更新:在下面的答案中提出建议后我的 CustomAdapter:

public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo>  {

private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck; 

public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
    super(context, NO_SELECTION);
    appInfoList = myList;
    ctx=context;
    mInflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = context.getPackageManager();

    positionArray = new ArrayList<Boolean>(myList.size());
    for(int i =0;i<myList.size();i++){
        positionArray.add(false);
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appInfoList.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    Holder holder = null;

    if(row==null){
        row = mInflater.inflate(R.layout.testlayout, null); 
        //  visiblePosArray[position%visiblePosArray.length]=position;
        holder = new Holder();
        holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);

        holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);

        row.setTag(holder);
    } else {

        holder = (Holder) convertView.getTag();
    }

    holder.ckbox.setFocusable(false);
    holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
    holder.ckbox.setChecked(positionArray.get(position));
    holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
    holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked ){
            System.out.println(position+"--- :)");
                positionArray.add(position, true);
            }else
                positionArray.add(position, false);
        }
    });

    return row;
}
static class Holder
{
    ImageView appIcon;
    CheckBox ckbox;

}

}

当我在 syso 中上下滚动时,我可以看到布尔数组列表中的随机索引更改为 true。

【问题讨论】:

  • 您需要单检模式还是多检模式?
  • 用户可以勾选多个复选框。

标签: android android-listview


【解决方案1】:

当列表视图回收视图时,它会回收其当前状态以及附加到它的侦听器。在我的示例中,如果复选框被选中并且设置了 onCheckedChangeListener,则两者都将根据位置保留为回收视图的一部分。所以我们有责任重置所有状态并移除之前的监听器。

因此,当我取消选中回收视图时,onCheckedChange 侦听器正在执行。 一行使程序完美运行。侦听器已被删除:

holder.ckbox.setOnCheckedChangeListener(null); 

以下是适配器的工作代码,供可能偶然发现此问题的人使用:

public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo>  {

private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck; 

public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
    super(context, NO_SELECTION);
    appInfoList = myList;
    ctx=context;
    mInflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = context.getPackageManager();

    positionArray = new ArrayList<Boolean>(myList.size());
    for(int i =0;i<myList.size();i++){
        positionArray.add(false);
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appInfoList.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    Holder holder = null;

    if(row==null){
        row = mInflater.inflate(R.layout.testlayout, null); 
        //  visiblePosArray[position%visiblePosArray.length]=position;
        holder = new Holder();
        holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);

        holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);

        row.setTag(holder);
    } else {

        holder = (Holder) convertView.getTag();
    holder.ckbox.setOnCheckedChangeListener(null);

    }

    holder.ckbox.setFocusable(false);
    holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
    holder.ckbox.setChecked(positionArray.get(position));
    holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
    holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked ){
            System.out.println(position+"--- :)");
                positionArray.set(position, true);

            }else
                positionArray.set(position, false);
        }
    });

    return row;
}
static class Holder
{
    ImageView appIcon;
    CheckBox ckbox;

}

}

【讨论】:

  • 请记住,每次将选中的更改侦听器设置为 null 都会消耗内存,因为您总是为每一行设置新的侦听器。 (即每次滚动时 100 行的 100 个侦听器),可以通过对我的答案进行的编辑来解决获得真实位置的方法。 (例如将位置标记为CheckBox
  • 嗨,我也有同样的问题。我可以知道Holderholder 是做什么的吗?
  • Holder 类保留对视图的引用,因此我们不需要进行昂贵的 findViewById 调用来获取视图引用。它让事情变得更有效率。
  • 两周后一直在寻找解决方案。这个工作非常出色,谢谢
  • 创建视图持有者的示例可以在here找到。
【解决方案2】:

您需要跟踪检查状态,因为ListView 重复使用Views。因此之前启用/禁用的位置 1 的状态可能与位置 7 的状态相同。

所以您需要做的是将选中状态保存在数组boolean 或您喜欢的任何内容中。

在构造函数中取一个类级别boolean [] checkedState;对其进行初始化,根据您的数据数组大小,您也可以使用ArrayList&lt;Boolean&gt;作为动态大小。

OnStateChangeListener 设置为getView() 中的CheckBoxes,无论何时选中或取消选中,获取该位置并将其保存在checkedState 的数组中,如下所示:

checkedState[position] = false;// or true accordingly

当为View 设置其他数据时,如TextViewImageView 为任何特定位置,也相应地设置选中状态,如下所示:

holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.ckbox.setChecked(checkedState[position]);

一个很好的解释和例子:

Android custom image gallery with checkbox in grid to select multiple

编辑:实际上正在发生的事情是,您的位置出现问题,要解决此问题,请添加以下行:

holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
holder.ckbox.setTag(String.valueOf(position));   // to properly track the actual position
holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton v, boolean isChecked) {
            int pos = Integer.parseInt( v.getTag().toString()) ; //to take the actual position
            positionArray.add(pos, isChecked);  // we don't need to check whether it is true or false, however you can put if-else to debug the app.

      }
});

【讨论】:

  • 我已经尝试过你的答案,但它似乎不起作用,当我向下和向上滚动时,随机复选框仍然会被选中。我正在发布我更新的适配器代码。我无法打开您提供的链接,因为它在我的工作场所被阻止。将在家里看到。
  • 职位即将到来。只是添加了随机位置。我找到了解决方案并发布了它。为您的努力 +1。
  • 我的问题是我在 if(convertView == null) 语句中做了很多这样的事情,当然它应该在外面 - 哦!感谢您的回答。
  • 6 年后,但这正是我的问题。这应该是公认的答案。将侦听器设置为 null 有效,但效率极低,并且会降低应用程序的速度。
【解决方案3】:

这两种方法的组合对我有用:

我有一个类级别的布尔数组,用于跟踪复选框的值。

boolean [] checkedItems = new boolean[listItems.size()];

在 getView() 中:

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.menu_item_list_item,
                    parent, false);

            holder = new ViewHolder();

            holder.name = (TextView) convertView
                    .findViewById(R.id.menuItemLargeName);
            holder.mainItemCheckBox = (CheckBox) convertView
                    .findViewById(R.id.menuItemLargeCheckBox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            // remove the listener so that it does not get attached to other chechboxes. 
            holder.mainItemCheckBox.setOnCheckedChangeListener(null);
            //update the checkbox value from boolean array
            holder.mainItemCheckBox.setChecked(checkedItems[position]);
        }


        holder.name.setText(listItems.get(position).getName());

        holder.mainItemCheckBox
                .setOnCheckedChangeListener(onCheckedListener);
        holder.mainItemCheckBox
                .setTag(R.id.menuItemLargeCheckBox, position);

        return (convertView);
    }

在我的 OnCheckedChangeListener() 中:更新布尔数组。

    OnCheckedChangeListener onCheckedListener = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        int position = (Integer) buttonView
                .getTag(R.id.menuItemLargeCheckBox);

        MenuItemObject menuItem = listItems.get(position);

        if (isChecked) {

            cartItems.add(menuItem);
            checkedItems[position] = true;

        } else {

            cartItems.remove(menuItem);
            checkedItems[position] = false;
        }


    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多