我正在使用viewholder工作自定义适配器 listview。我使用 + 增加和减少购物车项目>和 - 按钮在这里。当我增加购物车中的第一项时,它没有在textview中显示正确的增量值,我在那里显示它并没有正确更新增量值.Hereby我附加代码请做一些有用的。
public class OrderListAdapter extends BaseAdapter {
List<CartDetail> retrieveItems;
Context ctx;
ViewHolder viewHolder;
public OrderListAdapter(Context ctx,
ArrayList<CartDetail> rideDetails) {
// TODO Auto-generated constructor stub
this.retrieveItems = rideDetails;
this.ctx = ctx;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return retrieveItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return retrieveItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) ctx
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.order_cart_item, null);
}
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tvproductname);
viewHolder.tvNumber.setText(retrieveItems.get(position).getmQuantity());
minteger = Integer.parseInt(retrieveItems.get(position).getmQuantity());
viewHolder.btnDecrease.setTag(convertView);
viewHolder.btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (minteger == 1) {
minteger = minteger - 0;
viewHolder.tvNumber.setText("" + minteger);
} else {
minteger = minteger - 1;
View temp = (View) v.getTag();
strI = String.valueOf(minteger);
String value = retrieveItems.get(position).getmCart();
String productid = retrieveItems.get(position).getmProductId();
String itemTotal = retrieveItems.get(position).getmItemTotal();
String unitPrice = retrieveItems.get(position).getmUnitPrice();
Float intPrice = Float.parseFloat(unitPrice);
finalTotalProductAmount = intPrice * minteger;
TextView tv = (TextView)temp.findViewById(R.id.txtView);
tv.setText(""+minteger);
mTask = new ReduceCartItem(value, productid, itemTotal).execute();
}
}
});
viewHolder.btnIncrease.setTag(convertView);
minteger1=Integer.parseInt(retrieveItems.get(position).getmQuantity());
viewHolder.btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View temp = (View) v.getTag();
minteger1 = minteger1+ 1;
Log.e("integer",""+minteger1);
strI = String.valueOf(minteger1);
String value1 = retrieveItems.get(position).getmCartId();
String productid1 = retrieveItems.get(position).getmProductId();
String itemTotal1 = retrieveItems.get(position).getmItemTotal();
String unitPrice1 = retrieveItems.get(position).getmUnitPrice();
Float intPrice1 = Float.parseFloat(unitPrice1);
finalTotalProductAmount = intPrice1 * minteger1;
TextView tv = (TextView)temp.findViewById(R.id.txtView);
tv.setText(""+minteger1);
Log.e("posiiton", "" + retrieveItems.get(position).getmUnitPrice());
mTask = new UpdateItemTask(value1, productid1, itemTotal1, viewHolder.tvTotalvalue).execute();
}
});
strI = String.valueOf(minteger);
return convertView;
}
public class ViewHolder {
TextView tvProductName, tvProductID, tvUnitPrice, tvDiscount, tvTotal, tvUpdate, tvDelete, tvTotalvalue;
ImageView imageView;
EditText etQuantity;
Button btnIncrease, btnDecrease;
TextView tvNumber;
}
}