我是一个新手Android和工作在一个电子商务应用程序。我有一个产品的模型类(指定产品名称,价格等)。
public class Product implements Serializable {
/**
* The item Merchant Name.
*/
private String merchantName = "";
/**
* The item desc.
*/
private String description = "";
/**
* The mrp.
*/
private String mrp;
/**
* The discount.
*/
private String discount;
/**
* The image url.
*/
private int imageUrl;
private String productName = "";
private String productId = "";
public Product(String description, String mrp, String discount,
int imageUrl, String productName, String productId, String merchantName) {
this.description = description;
this.mrp = mrp;
this.discount = discount;
this.imageUrl = imageUrl;
this.productName = productName;
this.productId = productId;
this.merchantName=merchantName;
}
public void setMerchantName(String merchantName) {
this.merchantName = merchantName;
}
public void setDescription(String description) {
this.description = description;
}
public void setMrp(String mrp) {
this.mrp = mrp;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public void setImageUrl(int imageUrl) {
this.imageUrl = imageUrl;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getMerchantName(){
return merchantName; }
public String getDescription() {
return description;
}
public String getMrp() {
return mrp;
}
public String getDiscount() {
return discount;
}
public int getImageUrl() {
return imageUrl;
}
public String getProductName() {
return productName;
}
public String getProductId() {
return productId;
}
}
我想显示从数组列表到列表视图的产品。但是,如果有相同的产品(具有相同的productId)在数组列表中两次,我希望它显示在列表视图中,只有一行,数量-2(请参见图片)。有人能帮我吗?
下面是listview适配器类:
public class MyOrdersProducts_Adapter extends BaseAdapter {
Activity activity;
ArrayList<Product> cartproductsList;
Product tempValues;
public MyOrdersProducts_Adapter(Activity activity,ArrayList<Product> cartproductsList){
this.activity=activity;
this.cartproductsList=cartproductsList;
}
@Override
public int getCount() {
return cartproductsList.size();
}
@Override
public Object getItem(int i) {
return cartproductsList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class MyViewHolder{
CheckBox checkBox;
ImageView imageView;
LinearLayout listRow;
TextView price,title,quantity;
public MyViewHolder(View v){
checkBox=v.findViewById(R.id.checkbox_product);
imageView=v.findViewById(R.id.img_product_list);
price=v.findViewById(R.id.price_productlist);
title=v.findViewById(R.id.title_productlist);
quantity=v.findViewById(R.id.quantity_productlist);
listRow=v.findViewById(R.id.productlist_row);
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View rowView=view;
MyViewHolder holder;
if(rowView==null){
LayoutInflater inflater=activity.getLayoutInflater();
rowView=inflater.inflate(R.layout.myordersproducts_row,null);
holder=new MyViewHolder(rowView);
rowView.setTag(holder);
}
else
holder= (MyViewHolder) rowView.getTag();
tempValues=cartproductsList.get(i);
holder.imageView.setImageResource(tempValues.getImageUrl());
holder.title.setText(tempValues.getProductName());
holder.price.setText(tempValues.getMrp());
return rowView;
}
}
编辑:
请注意,我不能使用HashSet(用于创建唯一列表)或HashMap(用于映射具有整数计数值的产品),因为我不能从HashSets将数据设置为BaseAdapter。
在将数据添加到RecyclerView
之前过滤数据,例如:
// Beware, this is pseudo code
void addData(List<Product> items) {
HashMap<String, ProductItem> newItems = new HashMap<>();
for (ProductItem item: items) {
if (newItems.containsKey(item.id)) {
newItems.get(item.id).incrementQuantity();
} else {
newItems.put(item.id, item);
}
}
adapter.add(newItems.values())
}
在向ArrayList添加项目时,需要添加检查,以查看是否已经存在相同的产品。确保在product类中添加一个quantity字段,当您遇到重复项时,只需增加数量。
但是如果arraylist中有两个相同的产品(具有相同的productId)
为什么不避免在ArrayList
中添加具有相同产品id的产品呢?在
ArrayList
中添加任何产品之前,请检查ArrayList
中的任何现有产品是否具有相同的产品id
。如果存在具有相同产品id的产品,请不要添加该产品。这样,您将拥有一个具有唯一
产品id的
产品列表
。
我编写了一个代码,从txt文件中提取一行,将其拆分为不同的字符串和整数,然后将其存储到数组列表中,作为一个名为Professor的对象。主类的代码为: 问题是如何在控制台中显示arraylist?以及如何访问ArrayList中的一个对象中的字符串? 提前致谢
我目前有一个ArrayList,其中包含“Product”对象,每个对象中包含一个字符串、整数和double。我希望能够使用参数搜索对象,找出与搜索参数匹配的ID(整数值),并将此对象复制到另一个ArrayList中。有没有可能通过迭代器实现这一点,或者有没有更简单的方法?
问题内容: var arrObj = [{a:1, b:2},{c:3, d:4},{e:5, f:6}]; 如何将其合并为一个obj? 问题答案: 如果您的环境支持,那么您可以像这样简洁地进行操作 ES5解决方案: 您可以使用这样的 此解决方案仅将的所有键及其值收集在中的每个对象中,最终将结果返回给我们。 这张支票 有必要确保我们在结果中不包括所有继承的可枚举属性。
好的,我有这个 我一直在试着把它显示在视图中。 因为它不是一个json数组,所以angular只是使用 null 编辑 下面是完整的json
我有一个ApplicationInfo类型的列表和另一个String类型的ArrayList。我想在listView中列出这两者。我可以将ArrayAdapter(ApplicationInfo)和Array适配器(String)扩展到ApplicationAdapter吗。班这是我当前的代码,只显示ApplicationInfo。 ApplicationAdapter.class 所有应用程序活
id Number - 与显示相关联的唯一标识符。 rotationNumber -顺时针方向的屏幕旋转角度, 可选0,90,180,270。 scaleFactor Number - 输出设备的像素比例因子。 touchSupport String - 是否支持触摸,可选 available, unavailable, unknown. bounds Object Rectangle size