我正在开发购物车应用。其中有加号和减号按钮,用于在购物车中添加商品。
如果用户点击加号按钮,则TextView
应该从0
更改为1
或更多相同的事情,如果用户点击减号按钮,则值TextView
1}}应该减少。
我改变了值,但是它的增加和减少一次超过一行。
@SuppressLint("InflateParams")
public class ProductListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Product> products;
private ProductListAdapterListener listener;
int t;
int count = 0;
Context context;
List<Product> rowItems;
String[] result;
ViewHolder holder;
protected static final String TAG = "ReviewAdapter";
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private HashMap<Integer, Integer> mCountHash;
public ProductListAdapter(Activity activity, List<Product> feedItems,
ProductListAdapterListener listener) {
this.activity = activity;
this.products = feedItems;
this.listener = listener;
}
public static class ViewHolder {
TextView name, description, price, qty;
Button btnAddToCart, btnReduce;
NetworkImageView image;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int location) {
return products.get(location);
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resources resources = this.context.getResources();
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item_product, null);
final View vii = convertView;
final Product product = products.get(position);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.productName);
holder.description = (TextView) convertView
.findViewById(R.id.productDescription);
holder.qty = (TextView) convertView.findViewById(R.id.productQty);
holder.price = (TextView) convertView.findViewById(R.id.productPrice);
holder.image = (NetworkImageView) convertView
.findViewById(R.id.productImage);
// holder.qty=(TextView) convertView.findViewById(R.id.productQty);
holder.btnAddToCart = (Button) convertView
.findViewById(R.id.btnAddToCart);
holder.btnReduce = (Button) convertView.findViewById(R.id.btnReduce);
holder.name.setText(product.getName());
holder.description.setText(product.getDescription());
holder.price.setText("$" + product.getPrice());
// user profile pic
holder.image.setImageUrl("http://flavorbaba.com/adminpanel/images/"
+ product.getImage(), imageLoader);
holder.btnAddToCart.setTag(position);
holder.qty.setTag(position);
holder.btnAddToCart.setTag(holder);
// holder.btnReduce.setEnabled(false);
// holder.btnReduce.setVisibility(View.GONE));
holder.btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
listener.onAddToCartPressed(product);
View parentView = (View) vii.getParent();
// TextView tvqty = ((TextView) parentView
// .findViewById(R.id.textview1)).getText().toString();
TextView tvqty = ((TextView) parentView
.findViewById(R.id.productQty));
tvqty.setText(String.valueOf(count));
// Button btnAddToCart = (Button) v;
// holder.qty.setText(String.valueOf(count));
// notifyDataSetChanged();
Toast.makeText(activity, String.valueOf(position),
Toast.LENGTH_SHORT).show();
}
});
holder.btnReduce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// t = Integer.parseInt(holder.qty.getText().toString());
// holder.qty.setText(String.valueOf(t - 1));
listener.onReducePressed(product);
}
});
convertView.setTag(holder);
return convertView;
}
public interface ProductListAdapterListener {
public void onAddToCartPressed(Product product);
public void onReducePressed(Product product);
}
}
答案 0 :(得分:0)
希望此代码可以为您提供帮助。
plus = (Button) findViewById(R.id.btn_plus);
minus = (Button) findViewById(R.id.btn_minus);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
txt_number.setText(Integer.toString(textvalue));
textvalue++;
}
});
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textvalue--;
txt_number.setText(Integer.toString(textvalue));
}
});