这是我传递产品的适配器。
public class AdapterShoppingCartItem extends BaseAdapter {
private Context context;
private ArrayList<Product> products = new ArrayList();
public AdapterShoppingCartItem(Context context, ArrayList<Product> products) {
this.context = context;
this.products = products;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView productName;
TextView productMrp;
TextView productSellPrice;
TextView productItemInCart;
TextView productTotal;
Button plusButton;
Button minusButton;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_shopping_cart_item, null);
holder = new ViewHolder();
holder.productName = (TextView) convertView.findViewById(R.id.productName);
holder.productMrp = (TextView) convertView.findViewById(R.id.mrp);
holder.productSellPrice = (TextView) convertView.findViewById(R.id.sellPrice);
holder.productItemInCart = (TextView) convertView.findViewById(R.id.quantity);
holder.productTotal = (TextView) convertView.findViewById(R.id.total);
holder.plusButton = (Button) convertView.findViewById(R.id.plusButton);
holder.minusButton = (Button) convertView.findViewById(R.id.minusButton);
holder.minusButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Utility.displayToast("minus");
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Product product = (Product) getItem(position);
if (product != null) {
ArrayList<ProductVariant> productVariants = product.getProductVariants();
for(int j=0; j<productVariants.size(); ++j){
final ProductVariant productVariant = productVariants.get(j);
if(productVariant.getInCart() > 0){
String productName = product.getProductName()
+ " " + productVariant.getVariant().getVariantName();
holder.productName.setText(productName);
BigDecimal mrp = productVariant.getMrp();
holder.productMrp.setText(mrp.toString());
BigDecimal sellPrice = productVariant.getSellPrice();
holder.productSellPrice.setText(sellPrice.toString());
int quantity = productVariant.getInCart();
holder.productItemInCart.setText(Integer.toString(quantity));
Payment payment = new Payment();
BigDecimal total = payment.calculateCost(quantity, sellPrice);
holder.plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
productVariant.setInCart(1 + productVariant.getInCart());
notifyDataSetChanged();
Utility.displayToast("plus");
}
});
}
}
//if (holder.imageView != null) {
//new Utility.ImageDownloaderTask(holder.imageView).execute(String.valueOf(brand.getBrandImagePath()));
//}
//holder.imageView.setText(brand.getBrandImagePath());
} else {
Toast.makeText(context, "shoppingCartItem is null", Toast.LENGTH_SHORT).show();
}
return convertView;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return products.indexOf(getItem(position));
}
}
我只是想显示那些购物车中任何一个变量超过0的产品。但我的上述逻辑显示了很多空行,即使我保留了条件。
有人可以帮助解决逻辑错误吗?如何解决?
答案 0 :(得分:0)
删除For循环int getView()方法。正如您在getCount()中提到的产品大小。使用getview方法中的位置值循环显示Productvariant并显示带有购物车计数条件的项目。
ArrayList<ProductVariant> productVariants = product.getProductVariants();
final ProductVariant productVariant = productVariants.get(position);
if(productVariant.getInCart()>0)
{
convertView.setVisibility(View.VISIBLE);
String productName = product.getProductName()
+ " " + productVariant.getVariant().getVariantName();
holder.productName.setText(productName);
BigDecimal mrp = productVariant.getMrp();
holder.productMrp.setText(mrp.toString());
BigDecimal sellPrice = productVariant.getSellPrice();
holder.productSellPrice.setText(sellPrice.toString());
int quantity = productVariant.getInCart();
holder.productItemInCart.setText(Integer.toString(quantity));
Payment payment = new Payment();
BigDecimal total = payment.calculateCost(quantity, sellPrice);
holder.plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
productVariant.setInCart(1 + productVariant.getInCart());
notifyDataSetChanged();
Utility.displayToast("plus");
}
else
{
convertView.setVisibility(View.GONE);
}