0

I created a grid view with image view and checkbox inside of it, and I inflated my checkbox and image view in that grid view. If I checked my checkboxes and then scroll down/up the grid view, the checkboxes that is out to the grid has been checked became uncheck. What's wrong with my code

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    public ImageAdapter(Context c, boolean checkAll) {
        context = c;
        isCheckAll = checkAll;
    }

    public int getCount() {
        return count;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.a_6th_main_checkbox, null);


            holder.imageview = (ImageView) convertView.findViewById(R.id.img_thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbo_CheckImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();

        }


        holder.checkbox.setId(position);
        holder.imageview.setId(position);

        holder.checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                CheckBox cb = (CheckBox) v;
                int id = cb.getId();

                if(cb.isChecked()){
                    cb.setChecked(true);
                    thumbnailsselection[id] = true;

                }
                else if (!cb.isChecked()){
                    cb.setChecked(false);
                    thumbnailsselection[id] = false;
                }

            }
        });
        final int id = holder.checkbox.getId();
        holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                CheckBox check = (CheckBox) buttonView;

                if(buttonView.isChecked() == isChecked){

                    thumbnailsselection[id] = true;

                }
                else {
                    thumbnailsselection[id] = false;
                }
            }
        });
        holder.imageview.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);

            }
        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        if (!isCheckAll){
            holder.checkbox.setChecked(false);
        }else{
            holder.checkbox.setChecked(true);

        }

        holder.id = position;
        return convertView;
    }
}
class ViewHolder {
    CheckBox checkbox, cbo_SelectAll;
    ImageView imageview;
    int id;
}
}
NewDroidDev
  • 1,656
  • 5
  • 19
  • 33
  • http://stackoverflow.com/questions/17234399/in-gridview-checkbox-is-unchecked-while-scrolling-gridview-up-and-down/17234461#17234461. similar question. try this – Raghunandan Jul 08 '13 at 08:05

2 Answers2

0

After holder.checkbox.setId(position);

You should add :

holder.checkbox.setChecked(thumbnailsselection[id]);
DaveP
  • 6,952
  • 1
  • 24
  • 37
Bi Qin
  • 61
  • 3
  • That's not working every time I scroll up my grid view, the checkboxes that is checked and goes to the top and became out of sight became uncheck after I scroll down again – NewDroidDev Jul 08 '13 at 08:04
  • @NewDroidDev try the link i posted it works modify the same according to your requirements . i followed the source @ https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M and here's th sameple http://stackoverflow.com/questions/17234399/in-gridview-checkbox-is-unchecked-while-scrolling-gridview-up-and-down/17234461#17234461 – Raghunandan Jul 08 '13 at 08:08
  • ok, the question is isCheckAll, in the end of getView, u use a if (isCheckAll) to set holder.cb's checked – Bi Qin Jul 08 '13 at 08:14
0

You are setting the tag of the checkbox only if convertview is null. This happens only for the first screen of records. When user scrolls down, the previous convertviews are recycled. Thus your checkboxes have older data items as their tags.

Your checked change listener should look like this:

new CompoundButton.OnCheckedChangeListener()
{
   @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
    {
    Person element = (Person) viewHolder.checkBox.getTag();

    data[position].setCheck(isChecked);

    if(isChecked)
    {
        // do your stuff
    }
    else
    {
        //to-do
    }

}

}

DON"T use onCheckedChanged, use onClick instead, that works for me.

lolliloop
  • 389
  • 8
  • 23