How to show only checked ChekBox in android

197 Views Asked by At

Hello stackoverflow I'm trying to list down the image files from SD card using my own custom adapter using the following code

public class MyCustomArrayAdapter extends ArrayAdapter<Model> {

private final Activity context;
private final List<Model> list;
private static HashSet<String> checkedItems = new HashSet<String>();
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

public static HashSet<String> getCheckedItems() {
    return checkedItems;
}//End of getCheckedItems()

private static void setCheckedItems(String packageName) {
    if(!checkedItems.contains(packageName)){
        checkedItems.add(packageName);
    }else{
        checkedItems.remove(packageName);
    }
}//End of setCheckedItems()

public MyCustomArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.list_layout, list);
    this.context = context;
    this.list = list;

    for (int i = 0; i < list.size(); i++) {
        checkList.add(false);
    }
}//End of MyCustomArrayAdapter() constructor

static class ViewHolder {
    protected TextView text;
    protected ImageView image;
    protected ProgressBar pb;
    protected CheckBox cb;
}//End of ViewHolder static inner class

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.list_layout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.text.setTextColor(Color.BLACK);
        viewHolder.image = (ImageView) view.findViewById(R.id.image);
        viewHolder.image.setVisibility(View.GONE);
        viewHolder.cb = (CheckBox) view.findViewById(R.id.item_check_box);
        viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
        viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it
        viewHolder.pb = (ProgressBar) view.findViewById(R.id.progressBar);
        view.setTag(viewHolder);
    } else {
        view = convertView;
    }
    final int pos = position;
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.image.setTag(list.get(position).getURL());
    holder.image.setId(position);
    PbAndImage pb_and_image = new PbAndImage();
    pb_and_image.setImg(holder.image);
    pb_and_image.setPb(holder.pb);
    new DownloadImageTask().execute(pb_and_image);

    holder.image.setOnClickListener(new View.OnClickListener() 
    {               
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://"+list.get(pos).getURL()), "image/*");
            context.startActivity(intent);
        }//End of onClick method
    });//End of holder.image anonymous class

    holder.cb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setCheckedItems(list.get(pos).getURL());
        }
    });

    holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checkList.set((Integer)buttonView.getTag(),isChecked);
        }//End of onCheckedChanged()
    });//End of holder.cb anonymous class
    return view;
}//End of getView()
}//End of MyCustomArrayAdapter class

But the problem is when I check a CheckBox and scroll other check boxes also gets checked. Please help me to solve this riddle.

Thanks.

6

There are 6 best solutions below

1
On BEST ANSWER

Take these two lines out of the if block. They should always be executed:

viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it

Like this:

...
...
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.image.setTag(list.get(position).getURL());
holder.image.setId(position);
holder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
holder.cb.setChecked(checkList.get(position)); // set the status as we stored it
...
...

Since you set the tag and checked status of the checkbox only within the if, the values are not updated when you scroll. Hence the behavior is strange. This should be working..

0
On

use wrapper class and take one Boolean in that wrapper class and set checkbox according to that wrapper class Boolean value because we have to hold the value of check box bcoz when we scroll list view then the view is newly made and it cant store the value of that

0
On

set true to setChecked="true" make the checkbox checked

viewHolder.cb.setChecked="true";
0
On

You have to check or unchecked current Element in getView. Something like

holder.cb.setChecked(checkList.get((Integer)buttonView.getTag()));

That's because u reuse view and the view "remember" checkbox state with the last used object. That's why You have to override it for each element in getView

0
On
if(checkbox.isChecked){
..its display only checked checkbox
}

plz check this condition

0
On

Take these lines out of if

viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it

also you will need to add holder.cb.setOnCheckedChangeListener(null); before the 2 lines or else they will trigger the setOnCheckedChangeListener with the CheckBox if it is a recycled View