0

I have an GridView with custom items: an ImageView and an Checkbox on it. I want to setOnClickListener on ImageView, to check/uncheck the checkbox's state when image is clicked.

This is the adapter for gridView:

public class ProductAdapter extends ArrayAdapter<Product>{
    Context context;
    int layoutResourceId;
    Product datap[];

    public ProductAdapter(Context context, int layoutResourceId, Product datap[]){
        super(context, layoutResourceId, datap);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.datap = datap;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View row = convertView;
    ProductHolder holder = null;
    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ProductHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.gviv);
        holder.den = (TextView)row.findViewById(R.id.gvtv1);
        holder.pretN = (TextView)row.findViewById(R.id.gvtv2);
        holder.pretV = (TextView)row.findViewById(R.id.gvtv3);
        holder.cb = (CheckBox)row.findViewById(R.id.gcb);
        row.setTag(holder);
    }else{
        holder=(ProductHolder)row.getTag();
    }
    Product product = datap[position];
    holder.denumire.setText(product.den);
    holder.imgIcon.setImageResource(product.icon);
    holder.imgIcon.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // THIS DOESN'T WORK!!!!!
            holder.cb.setChecked(true);

        }
    });

    holder.pretN.setText(product.pretN);
    holder.pretV.setText(product.pretV);
    return row;

}

static class ProductHolder{
    ImageView imgIcon;
    TextView den;
    TextView pretN;
    TextView pretV;
    CheckBox cb;
}

}
Ion C
  • 213
  • 3
  • 13
  • 24

1 Answers1

0

call notifyDataSetChanged(); after your check command.

        public void onClick(View v) {
            // THIS DOESN'T WORK!!!!!
            holder.cb.setChecked(true);
            notifyDataSetChanged();
        }
mango
  • 5,577
  • 4
  • 29
  • 41
  • can you add a simple sample? – Ion C Dec 16 '12 at 13:57
  • that's not work... "Cannot refer to a non-final variable holder inside an inner class defined in a different method" – Ion C Dec 16 '12 at 21:47
  • all i've done is show you where to place the `notifyDataSetChanged();` command. this is your code that i've pasted here. if it was working before, this won't affect it. – mango Dec 17 '12 at 00:08