我有自定义列表视图,我想以两种方式选择复选框,第一种是用户点击复选框,另一种是用户点击列表视图项目。
这是代码:
public View getView(int pos, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final int position = pos;
User p = getProduct(position);
if (convertView == null) {
holder = new ViewHolder();
vgparent = parent;
convertView = vi.inflate(R.layout.row2, vgparent, false);//null);
holder.name = (TextView) convertView.findViewById(R.id.tv_prodotto);
holder.qty_ing = (TextView) convertView.findViewById(R.id.tv_qta1);
holder.qty_risc = (TextView) convertView.findViewById(R.id.tv_qta2);
holder.cbView = (CheckBox) convertView.findViewById(R.id.cbBox);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
if(holder.name != null) {
holder.name.setText(data.get(position).getName());
/////////////////////////////////////////////////////////////////////////
//
// this is the problem
//
//
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder holder = null;
holder = new ViewHolder();
View convertView = vi.inflate(R.layout.row2, vgparent, false);
holder.cbView = (CheckBox) convertView.findViewById(R.id.cbBox);
holder.cbView.setChecked(true);
}
});
}
//////////////////////////////////////////////////////
if(holder.qty_ing != null) {
// verifica la coincidenza per poter cambiare colore
if(data.get(position).getName().equals(nameing))
holder.qty_ing.setTextColor(context.getResources().getColor(R.color.red));
else
holder.qty_ing.setTextColor(context.getResources().getColor(R.color.black));
holder.qty_ing.setText(String.valueOf(data.get(position).getQtyIng()));
}
if(holder.qty_risc != null) {
holder.qty_risc.setText(String.valueOf(data.get(position).getQtyRis()));
}
if(holder.cbView != null) {
holder.cbView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
if(isChecked){
qty_risc += GetDoubleVal(data.get(position).getQtyRis());
qty_ing += GetDoubleVal(data.get(position).getQtyIng());
}else {
qty_risc -= GetDoubleVal(data.get(position).getQtyRis());
qty_ing -= GetDoubleVal(data.get(position).getQtyIng());
}
listener.onTotalChanged(qty_risc, qty_ing);
}
});
holder.cbView.setTag(position);
holder.cbView.setChecked(p.box);
}
return convertView;
}
我已经实现了复选框点击并且工作正常,但另一种方式(点击列表视图的项目)对我来说非常困难。