recyclerview.addOnItemTouchListener 我需要将多个产品添加到购物车 recyclerview
recyclerview.addOnItemTouchListener i need to add multiple products to a cart recyclerview
我只能将一种产品发送到 PlaceOderActivity。我需要多个产品购物车
我需要多个产品来 placeOder
recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ProductsList selItem = listItems.get(position);
final String[] strings = {
selItem.getProductName(),
selItem.getSalePrice(),
String.valueOf(selItem.getQnty()),
};
Log.w(TAG, Arrays.toString(strings));
final String itemname = listItems.get(position).getProductName();
final float price = Float.parseFloat(listItems.get(position).getSalePrice());
final int qnty = listItems.get(position).getQnty();
ButtonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ProductListActivity.this,PlaceOderActivity.class);
intent.putExtra("itemname",itemname);
intent.putExtra("Price",price);
intent.putExtra("qnty",qnty);
intent.putExtra("strings",strings);
startActivity(intent);
}
});
}
})
);
基本上您所做的只是跟踪用户添加到购物车的最后一个产品。
每次您的代码到达您的 RecyclerItemClickListener 时,您都会用新的项目替换之前的项目。
您需要声明的是(作为class 属性):
private List<Product> productsList = new ArrayList()
每次用户点击添加产品时,您只需将其添加到列表中,如下所示:
productsList.add(Product)
然后您可以序列化和反序列化 Intent extras 中的列表,甚至可以将序列化的字符串保存到缓存文件中,您可以在下一个 activity.
中加载该文件
这是一个更好的例子:
public class Product {
/** Property name */
String name;
/** Property salePrice */
String salePrice;
/** Property quantity */
String quantity;
/**
* Constructor
*/
public Product() {
}
/**
* Gets the name
*/
public String getName() {
return this.name;
}
/**
* Sets the name
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the salePrice
*/
public String getSalePrice() {
return this.salePrice;
}
/**
* Sets the salePrice
*/
public void setSalePrice(String value) {
this.salePrice = value;
}
/**
* Gets the quantity
*/
public String getQuantity() {
return this.quantity;
}
/**
* Sets the quantity
*/
public void setQuantity(String value) {
this.quantity = value;
}
}
现在您需要 ProductListActivity 中的产品列表,每次用户将产品添加到购物车时,您都会使用 list.add() 而不是在 ModelCart 中使用您的 setter 每次都会替换列表。
我只能将一种产品发送到 PlaceOderActivity。我需要多个产品购物车
我需要多个产品来 placeOder
recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ProductsList selItem = listItems.get(position);
final String[] strings = {
selItem.getProductName(),
selItem.getSalePrice(),
String.valueOf(selItem.getQnty()),
};
Log.w(TAG, Arrays.toString(strings));
final String itemname = listItems.get(position).getProductName();
final float price = Float.parseFloat(listItems.get(position).getSalePrice());
final int qnty = listItems.get(position).getQnty();
ButtonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ProductListActivity.this,PlaceOderActivity.class);
intent.putExtra("itemname",itemname);
intent.putExtra("Price",price);
intent.putExtra("qnty",qnty);
intent.putExtra("strings",strings);
startActivity(intent);
}
});
}
})
);
基本上您所做的只是跟踪用户添加到购物车的最后一个产品。
每次您的代码到达您的 RecyclerItemClickListener 时,您都会用新的项目替换之前的项目。
您需要声明的是(作为class 属性):
private List<Product> productsList = new ArrayList()
每次用户点击添加产品时,您只需将其添加到列表中,如下所示:
productsList.add(Product)
然后您可以序列化和反序列化 Intent extras 中的列表,甚至可以将序列化的字符串保存到缓存文件中,您可以在下一个 activity.
中加载该文件这是一个更好的例子:
public class Product {
/** Property name */
String name;
/** Property salePrice */
String salePrice;
/** Property quantity */
String quantity;
/**
* Constructor
*/
public Product() {
}
/**
* Gets the name
*/
public String getName() {
return this.name;
}
/**
* Sets the name
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the salePrice
*/
public String getSalePrice() {
return this.salePrice;
}
/**
* Sets the salePrice
*/
public void setSalePrice(String value) {
this.salePrice = value;
}
/**
* Gets the quantity
*/
public String getQuantity() {
return this.quantity;
}
/**
* Sets the quantity
*/
public void setQuantity(String value) {
this.quantity = value;
}
}
现在您需要 ProductListActivity 中的产品列表,每次用户将产品添加到购物车时,您都会使用 list.add() 而不是在 ModelCart 中使用您的 setter 每次都会替换列表。