这里其他的代码就不贴了,主要就是adapter中的代码。
大概的思路就是把对应的值放到对应控件的tag中去,当需要显示的时候拿出来用,类似于listview解决imageView错乱的问题。方法都差不多,在这里记录下。
关于checkbox错乱:
viewHolder.checkBox.setChecked(bean.isChecked());
viewHolder.checkBox.setTag(bean);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean hold = ((InvoiceDetialsBean) v.getTag()).isChecked();
((InvoiceDetialsBean) v.getTag()).setChecked(!hold);
}
});
关于editeview错乱:
viewHolder.etHwhyslwmc.setText(bean.getHwhyslwmc());
viewHolder.etHwhyslwmc.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setHwhyslwmc(s.toString() + "");
}
});
对应的textwather:
private abstract class MyTextWatcher implements TextWatcher {
private ViewHolder mHolder;
public MyTextWatcher(ViewHolder holder) {
this.mHolder = holder;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
afterTextChanged(s, mHolder);
}
public abstract void afterTextChanged(Editable s, ViewHolder holder);
}
全部的adapter代码如下:
public class InvoiceDetailLvAdapter extends AppBaseAdapter {
private List list;
private Context context;
private TotalPriceCallBack mCallBack;
public interface TotalPriceCallBack {
void totalPrice();
}
public List getDataList() {
return list;
}
public InvoiceDetailLvAdapter(List list, Context context, TotalPriceCallBack callBack) {
super(list, context);
this.list = list;
this.context = context;
this.mCallBack = callBack;
}
@Override
public View getItemView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_invoice_detail_lv, parent, false);
viewHolder = new ViewHolder();
viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll_item_invoice_detail);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_item_invoice_detail);
viewHolder.etHwhyslwmc = (EditText) convertView.findViewById(R.id.et_item_invoice_detail_hwhyslwmc);
viewHolder.etGgxh = (EditText) convertView.findViewById(R.id.et_item_invoice_detail_ggxh);
viewHolder.spinnerJldw = (Spinner) convertView.findViewById(R.id.spinner_item_invoice_detail_jldw);
viewHolder.etNumber = (EditText) convertView.findViewById(R.id.et_item_invoice_detail_number);
viewHolder.etDj = (EditText) convertView.findViewById(R.id.et_item_invoice_detail_dj);
viewHolder.etHsje = (EditText) convertView.findViewById(R.id.et_item_invoice_detail_hsje);
viewHolder.spinnerSl = (Spinner) convertView.findViewById(R.id.spinner_item_invoice_detail_sl);
viewHolder.tvSe = (TextView) convertView.findViewById(R.id.tv_item_invoice_detail_se);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final InvoiceDetialsBean bean = list.get(position);
viewHolder.checkBox.setChecked(bean.isChecked());
viewHolder.checkBox.setTag(bean);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean hold = ((InvoiceDetialsBean) v.getTag()).isChecked();
((InvoiceDetialsBean) v.getTag()).setChecked(!hold);
}
});
//设置 货物或应税劳务名称
viewHolder.etHwhyslwmc.setText(bean.getHwhyslwmc());
viewHolder.etHwhyslwmc.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setHwhyslwmc(s.toString() + "");
}
});
//设置规格型号
viewHolder.etGgxh.setText(bean.getGgxh());
viewHolder.etGgxh.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setGgxh(s.toString() + "");
}
});
//设置数量
viewHolder.etNumber.setText(bean.getNumber());
viewHolder.etNumber.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setNumber(s.toString() + "");
}
});
//设置单价
viewHolder.etDj.setText(bean.getDj());
viewHolder.etDj.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setDj(s.toString() + "");
}
});
//设置含税金额
viewHolder.etHsje.setText(bean.getHsje());
viewHolder.etHsje.addTextChangedListener(new MyTextWatcher(viewHolder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
((InvoiceDetialsBean) holder.checkBox.getTag()).setHsje(s.toString() + "");
mCallBack.totalPrice();
}
});
return convertView;
}
private abstract class MyTextWatcher implements TextWatcher {
private ViewHolder mHolder;
public MyTextWatcher(ViewHolder holder) {
this.mHolder = holder;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
afterTextChanged(s, mHolder);
}
public abstract void afterTextChanged(Editable s, ViewHolder holder);
}
class ViewHolder {
private LinearLayout ll;
private CheckBox checkBox;
private EditText etHwhyslwmc;
private EditText etGgxh;
private Spinner spinnerJldw;
private EditText etNumber;
private EditText etDj;
private EditText etHsje;
private Spinner spinnerSl;
private TextView tvSe;
}
}