我有列表视图想要在特定行的列表视图中增加和减少产品数量。指导我这样做
public class ProductsAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> postItems;
public SharedPreferences settings;
public final String PREFS_NAME = "Products";
private MyCart cart;
private HashMap<String, String> selected_product;
DisplayImageOptions options;
ImageLoaderConfiguration imgconfig;
private static int _counter = 1;
private String _stringVal;
public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist){
this.context = context;
File cacheDir = StorageUtils.getCacheDirectory(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.loading)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new SimpleBitmapDisplayer())
.imageScaleType(ImageScaleType.EXACTLY)
.build();
imgconfig = new ImageLoaderConfiguration.Builder(context)
.build();
ImageLoader.getInstance().init(imgconfig);
postItems = arraylist;
settings = context.getSharedPreferences(PREFS_NAME, 0);
selected_product = new HashMap<String,String>();
cart=new MyCart(context.getApplicationContext());
}
@Override
public int getCount() {
return postItems.size();
}
@Override
public Object getItem(int position) {
return postItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_products, null);
}
HashMap<String, String> map = postItems.get(position);
ImageView imgProduct = (ImageView)convertView.findViewById(R.id.proimage);
ImageLoader.getInstance().displayImage(ConstValue.PRO_IMAGE_BIG_PATH+map.get("image"), imgProduct, options, animateFirstListener);
TextView txtTitle = (TextView)convertView.findViewById(R.id.proTitle);
txtTitle.setText(map.get("title"));
TextView txtPrice = (TextView)convertView.findViewById(R.id.txtprice);
txtPrice.setText(map.get("price"));
TextView textDiscount = (TextView)convertView.findViewById(R.id.textDiscount);
textDiscount.setVisibility(View.GONE);
TextView txtDiscountFlag = (TextView)convertView.findViewById(R.id.textDiscountFlag);
txtDiscountFlag.setVisibility(View.GONE);
TextView textCurrency = (TextView)convertView.findViewById(R.id.textCurrency);
textCurrency.setText(map.get("currency"));
final TextView value = (TextView)convertView.findViewById(R.id.textView3);
final Button txtminus = (Button) convertView.findViewById(R.id.minus);
txtminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("src", "Decreasing value...");
_counter--;
_stringVal = Integer.toString(_counter);
value.setText(_stringVal);
selected_product = (HashMap<String, String>) postItems.get(position);
selected_product.put("qty", _stringVal);
cart.remove_to_cart(selected_product);
}
});
Button txtplus = (Button) convertView.findViewById(R.id.plus);
txtplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("src", position + "Increasing value...");
Log.d("Position", position + "position");
_counter++;
_stringVal = Integer.toString(_counter);
value.setText(_stringVal);
selected_product = (HashMap<String, String>) postItems.get(position);
selected_product.put("qty", _stringVal);
cart.add_to_cart(selected_product);
}
});
public class ProductsAdapter extends BaseAdapter {
答案 0 :(得分:0)
您正在进行的当前设置只有一个计数器变量,您需要为每个列表视图行设置一个变量。我建议创建一些将按钮映射到位置值的哈希映射,所以:
private HashMap<Button, Integer> decrementButtons;
private HashMap<Button, Integer> incrementButtons;
private HashMap<Integer, Integer> positionCount;
public view getView(final int position, ....) {
... //Stuff above
decrementButtons.put(txtminus, position);
incrementButtons.put(txtplus, position);
if (!positionCount.contains(position)) {
positionCount.put(position, 0);
}
}
然后在你的onClick方法中添加:
public void onClick(View v) {
...
Button button = (Button) v;
int position = decrementButtons.get(button); //Change for increment
int count = positionCount.get(position);
count--;
positionCount.put(position, count);
...
}