简体   繁体   中英

Android - Unable to check all the CheckBoxes in a custom listview because of recycling issue?

I have a custom listview adapter with a imageview, textview and a checkbox. and i also have a button and a checkbox in my main layout(not in listview).

What here i want is to check all these listview checkboxes at once when i check my main layout's checkbox.

And i am saving the state of these checkbox in boolean type arraylist so i am able to check these checkboxes when i check one by one but so far i did not find a single way to check all these checkbox at once which i can use in my main layout's checkbox's OnCheckedChanged event.

i tried few techniques but because of ListView's recycling procedure it checks only those items which are currently visible and as i scroll down to my list, all the above items set to unchecked and some random items got checked.

how to get over this recycle thing and how to check all these checkboxes at once???? any advise? solution? idea?

here is a code of my Custom Adapter : -

public class IconAdapter extends BaseAdapter
{ 
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private ArrayList<String> itemSelected = new ArrayList<String>();
private ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();

public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
    activity = a;
    listItems = items;
    data = items.toArray();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = a.getPackageManager();
    for(int i = 0; i < items.size(); i++)
    {
        itemChecked.add(i,false);
    }
    for(int i = 0; i < items.size(); i++)
    {
        itemSelected.add(i," ");
    }

}

public int getCount() {
    return listItems.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView textView;
    public ImageView imageView;
    public CheckBox checkBox;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    ViewHolder holder;
    if(convertView==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check);
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        holder.checkBox.setTag(position);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }


    String s = data[position].toString();
    String[] tokens = s.split(",");
    String[] mToken = tokens[0].split("=");
    String taskName = mToken[1];
    String[] mTokens = tokens[1].split("=");
    final String pkgName =  mTokens[1].substring(0, (mTokens[1].length() - 1));


    holder.textView.setText(taskName);



    ctv.add(holder.checkBox);
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton button, boolean b) {
            //int posClicked = ((Integer)button.getTag());
            if(b)
            {
                itemChecked.set(position, true);
                itemSelected.set(position, pkgName);

            }
            else
            {
                itemChecked.set(position,false);

            }
        }
    });





    holder.checkBox.setChecked(itemChecked.get(position));




    try{
        Drawable icon =   pm.getApplicationIcon(pkgName);
        holder.imageView.setImageDrawable(icon);
    }
    catch (PackageManager.NameNotFoundException ne)
    {

    }
     row.setId(position);
     return row;
}

public boolean isChecked(int position)
{
    return itemChecked.get(position);
}
public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void removeItem(int position)
{
    listItems.remove(position);
}




public void setItemChecked(boolean isChecked)
{
    if(isChecked)
    {
        for(int i = 0; i < ctv.size(); i++)
        {

            ctv.get(i).setChecked(true);
        }
    }
    else
    {
        for(int i = 0; i < ctv.size(); i++)
        {

            ctv.get(i).setChecked(false);
        }
    }
}

as you can see in the end of this adapter i made a function setItemChecked() but this function is also a victim of recycle process as its only checking the visible item and as i scroll down it starts misbehaving.

any help would be great.

Thanks.

This morning I read an answer of an issue like this, and they recommend we put holder.checkBox.setChecked(itemChecked.get(position)); before holder.checkBox.setOnCheckedChangeListener

So, getView function will be rewritten like this:

public View getView(....)
{
    ...
    holder.checkBox.setChecked(itemChecked.get(position));///move to here
    holder.checkBox.setOnCheckedChangeListener(...);
    ...
)

Yeah, this recycling is a pain.

Here's what I did: http://dev.kafol.net/2011/11/android-checkbox-listview-un-check-all.html

I'm still having some issues with SharedPreferences, as allthough I have managed to get all the checkboxes checked or unchecked, it still doesn't save the state to sharedpreferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM