Android 在列表视图中选中一个复选框会使其他随机复选框也被选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11190390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:16:18  来源:igfitidea点击:

Checking a checkbox in listview makes other random checkboxes checked too

androidandroid-listview

提问by Akhil

whenever i check a checkbox in my listview , other random checkboxes get checked too . It could be due to item recycling by listview.

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

I also tried setting android:focusable="false"to checkbox in my layout as suggested in some places, but still the onListItemClick() is not called for a row when its checkbox is checked.Only when I click somewhere else it gets called.

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

What I want is that only the user-checked checkboxes should remain checked until the user unchecks them.

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

I give below the code which is complete and could be run directly.

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

Activity code- ProjActivity.java:

活动代码- 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);
}}

And finally, the custom layout file- testlayout.xml

最后,自定义布局文件 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"
    />

UPDATE : My CustomAdapter after the suggestion in an answer below:

更新:我的 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;

}

}

}

When I scroll up and down I could see random indices changed to true in my boolean Arraylist when in syso them.

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

回答by Akhil

When a listview recycles views , it recycles its present state as well aslisteners attached to it. In my example, if the checkbox was checked and has a onCheckedChangeListener set, both will remain a part of recycled view based on position. So it is our responsibility to reset all states and remove previous listeners.

当 listview 回收 views 时,它会回收其当前状态以及附加到它的侦听器。在我的示例中,如果复选框被选中并设置了 onCheckedChangeListener,则两者都将保留为基于位置的回收视图的一部分。因此,我们有责任重置所有状态并删除以前的侦听器。

So when I was unchecking the recycled view, the onCheckedChange listener was getting executed. one line made the program work perfectly. The listener was removed by :

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

holder.ckbox.setOnCheckedChangeListener(null); 

Below is the working code of Adapter for people who may stumble upon this problem:

以下是适配器的工作代码,供可能会遇到此问题的人使用:

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;

}

}

}

回答by Adil Soomro

You need to keep track of the check state, because ListViewre-uses the Views. so the state for position one which was previously enabled/disabled may appear as is for position 7.

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

So what you need to do is keep the checked state in an array booleanor whatever you prefer.

因此,您需要做的是将检查状态保存在数组中boolean或您喜欢的任何内容中。

Take a class level boolean [] checkedState;initialize it in constructor, according to your data array size, you can use ArrayList<Boolean>too for dynamic size.

boolean [] checkedState;在构造函数中以类级别初始化它,根据您的数据数组大小,您也可以使用ArrayList<Boolean>动态大小。

set OnStateChangeListenerto your CheckBoxesin getView(), whenever it is checked or un-checked, take the position and save it in the array of checkedStatelike this:

设置OnStateChangeListener为您的CheckBoxesin getView(),无论何时选中或取消选中,取位置并将其保存在如下数组中checkedState

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

and when setting other data for Viewlike TextViewor ImageViewfor any specific position, set the checked state also accordingly like this:

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

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

A very good explanation and example:

一个很好的解释和例子:

Android custom image gallery with checkbox in grid to select multiple

Android 自定义图片库,在网格中带有复选框以选择多个

Edit:Actually what is happening is, you position is getting buggy, to solve this add these lines:

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

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.

      }
});

回答by Sayyam

A combination of these two approaches worked for me:

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

I have a boolean array on class level which I use to keep track of the value of checkboxes.

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

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

In getView() :

在 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);
    }

In my OnCheckedChangeListener() : update the boolean array.

在我的 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;
        }


    }
};