我正在我的应用程序中实现一个购物车。在我的购物车视图中,我有一个带有 editText 的产品列表,因此用户可以更改其数量。我需要监听“数量变化”事件,以便相应地更新我的购物车对象。
我正在尝试使用 onTextChanged 事件,但它没有按预期工作。就在我打开购物车视图时,该事件被触发了数十次,并完全破坏了我的产品数量值。
我怎样才能确保值真的改变了?
这是我的代码:
我的 CartItemsListAdapter(负责渲染购物车的每一行)。
public class CartItemsListAdapter extends ArrayAdapter<CartItem> implements OnClickListener{
private ArrayList<CartItem> cartItems;
/**
* view holder for caching the view elements
*/
static class CartItemViewHolder {
TextView tvProductName;
TextView tvShopName;
TextView tvSubTotal;
EditText edQuantity;
Button btnRemoveFromCart;
ImageView ivProduct;
}
/**
* default constructor
* @param context
* @param textViewResourceId
* @param cartItems
*/
public CartItemsListAdapter(Context context, int textViewResourceId,
ArrayList<CartItem> cartItems) {
super(context, textViewResourceId, cartItems);
this.cartItems = cartItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CartItemViewHolder viewHolder;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.cart_item, null);
viewHolder = new CartItemViewHolder();
viewHolder.tvProductName = (TextView) v.findViewById(R.id.cart_item_product_name);
viewHolder.tvShopName = (TextView)v.findViewById(R.id.cart_item_shop_name);
viewHolder.tvSubTotal= (TextView)v.findViewById(R.id.cart_item_subtotal);
viewHolder.edQuantity = (EditText)v.findViewById(R.id.cart_item_product_quantity);
//viewHolder.btnRemoveFromCart = (Button)v.findViewById(R.id.cart_item_remove_item);
viewHolder.ivProduct = (ImageView)v.findViewById(R.id.cart_item_product_image);
v.setTag(viewHolder);
}else{
viewHolder = (CartItemViewHolder) v.getTag();
}
CartItem item = cartItems.get(position);
if(item != null){
viewHolder.tvProductName.setText(item.getProduct().getName());
viewHolder.edQuantity.setText(Integer.toString(item.getQuantity()));
viewHolder.tvSubTotal.setText(Double.toString(item.getSubTotal()));
viewHolder.tvShopName.setText(item.getProduct().getShop().getName());
UrlImageViewHelper.setUrlDrawable(viewHolder.ivProduct, item.getProduct().getImage());
viewHolder.edQuantity.addTextChangedListener(new CartItemQuantityChangeListener(cartItems,item));
}
return v;
}
@Override
public int getCount() {
return cartItems.size();
}
@Override
public CartItem getItem(int position) {
return cartItems.get(position);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
我的 TextChanged 监听器类
public class CartItemQuantityChangeListener implements TextWatcher {
private ArrayList<CartItem> cartItems;
private CartItem item;
public CartItemQuantityChangeListener(ArrayList<CartItem> cartItems,
CartItem item) {
this.cartItems = cartItems;
this.item = item;
}
public void afterTextChanged(Editable s) {
try {
int quantity = Integer.parseInt(s.toString());
Log.d(Constants.LOG_TAG, "quantity changed" + quantity);
int index = this.cartItems.indexOf(item);
Log.d(Constants.LOG_TAG, "INDEX" + index);
// this.cartItems.get(index).setQuantity(quantity);
/*
* for(CartItem i : this.cartItems){
* Log.d(Constants.LOG_TAG,item.getProduct().getName() + " : " +
* i.getQuantity()); }
*/
} catch (NumberFormatException ex) {
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}