Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

android - Checking a checkbox in listview makes other random checkboxes checked too

whenever i check a checkbox in my listview , other random checkboxes get checked too . It could be due to item recycling by 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.

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:

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

<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:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When a listview recycles views , it recycles its present state as well as listeners 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.

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 :

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;

}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...