我有一个带有自定义项目的 GridView:一个 ImageView 和一个 Checkbox。我想在 ImageView 上设置 OnClickListener,以在单击图像时选中/取消选中复选框的状态。
这是 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;
}
}