如何在一个ViewHolder中实现两个对象?

时间:2016-05-03 15:16:07

标签: android android-viewholder

我有两个清单,购物车和促销活动。我希望两者都显示在一个视图中。有什么办法可以吗?这就是我的做法,但它没有用。

   public class CartAdapter extends BaseAdapter {

    ArrayList<Cart> list1;

    Context context;

    public CartAdapter(Context context, ArrayList<Cart> list) {
        this.context = context;
        this.list1 = list;
    }

   @Override
    public int getCount() {
        return list1.size();
    }

    @Override
    public Object getItem(int position) {
        return list1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list1.get(position).getProductId();
    }

   public View getView(final int position, View convertView, ViewGroup   parent) {

    View row = convertView;
    final ViewHolder holder;

    if (row == null) {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       row = inflater.inflate(R.layout.cart_item, parent, false);

       holder = new ViewHolder(row);
       row.setTag(holder);
   } else {
        holder = (ViewHolder) row.getTag();
   }
   final Cart cart = list.get(position);
   final Promos promo = databaseHandler.getPromosForCart(cart);

   holder.tvTitle.setText(cart.getProductName());
   holder.tvPrice.setText(cart.getPrice());
   holder.etQuantity.setText(cart.getQuantity());
   holder.tvPromotion.setVisibility(View.GONE);

   if(cart.hasPromotion){
       final ViewHolder holder2;
       if (row == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

          row = inflater.inflate(R.layout.cart_item, parent, false);

          holder2 = new ViewHolder(row);
         row.setTag(holder);
      } else {
            holder2 = (ViewHolder) row.getTag();
      }

     if(promo.type().equals("type1"){
       holder2.tvTitle.setText(promo.getProductName());
       holder2.tvPrice.setVisibility(View.GONE);
       holder2.etQuantity.setVisibility(View.GONE);
       holder2.tvPromotion.setText(promo.getPromoDetails());
     }else{
       holder2.tvTitle.setText(promo.getProductName());
       holder2.tvPrice.setVisibility(View.GONE);
       holder2.etQuantity.(promo.getQuantity());
       holder2.etQuantity.setEnabled(false);
       holder2.tvPromotion.setText(promo.getPromoDetails());
    }
  }
 }
  Class ViewHolder{
  .....
    ViewHolder(View v){}
  }
}

我搜索了但是我得到的所有答案都是关于使用两个布局,并且只使用一个数据列表。请帮帮我。

0 个答案:

没有答案