onBindViewHolder()无法获得价值

时间:2018-10-22 09:37:25

标签: android android-viewholder

我没有在购物车中获取详细信息,这是我的代码

 //line no 20
     public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {

Context context;
List<Cart> cartList;

public CartAdapter(Context context, List<Cart> cartList) {
    this.context = context;
    this.cartList = cartList;
}

@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView= LayoutInflater.from(context).inflate(R.layout.cart_item_layout,parent,false);
    return new CartViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
    Picasso.with(context)
            .load(cartList.get(position).link)
     //line no 41       .into(holder.img_product);

    holder.txt_amount.setNumber(String.valueOf(cartList.get(position).amount));
    holder.txt_price.setText(new StringBuilder("Rs-").append(cartList.get(position).price));
    holder.txt_product_name.setText(cartList.get(position).name);
    holder.txt_sugar_ice.setText(new StringBuilder("Sugar: ")
            .append(cartList.get(position).sugar).append("%").append("\n")
            .append("Ice: ").append(cartList.get(position).ice)
            .append("%").toString());

    //Auto save item when user change amount
    holder.txt_amount.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
        @Override
        public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
            Cart cart= cartList.get(position);
            cart.amount=newValue;

            Common.cartRepository.updateCart(cart);
        }
    });

}

@Override
public int getItemCount() {
    return cartList.size();
}

class CartViewHolder extends RecyclerView.ViewHolder
{
    ImageView img_product;
    TextView txt_product_name,txt_sugar_ice,txt_price;
    ElegantNumberButton txt_amount;


    public CartViewHolder(View itemView) {
        super(itemView);

        img_product=(ImageView)itemView.findViewById(R.id.img_product);

txt_product_name=TextView)itemView.findViewById(R.id.txt_product_name);
        txt_sugar_ice=(TextView)itemView.findViewById(R.id.txt_sugar_ice);
        txt_price=(TextView)itemView.findViewById(R.id.txt_price);
        txt_amount=(ElegantNumberButton) itemView.findViewById(R.id.txt_amount);
    }
}

}

添加到购物车后,我选择了我的产品,当我按购物车图标时,它显示错误,目标不能为空。   在com.developers.a_g.designapp.Adapter.CartAdapter.onBindViewHolder(CartAdapter.java:41)  在com.developers.a_g.designapp.Adapter.CartAdapter.onBindViewHolder(CartAdapter.java:20)

cart_item_layout

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="8dp"
    >



 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="horizontal">



   <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_product_name"
            android:text="Milk Tea"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/txt_sugar_ice"
            android:text="Milk Tea"
            android:textSize="14sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="7">

            <TextView
                android:id="@+id/txt_price"
                android:text="RS-500"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                />
            <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
                android:layout_width="80dp"
                android:layout_height="30dp"
                android:id="@+id/txt_amount"
                app:initialNumber="1"
                android:layout_weight="1"
                app:finalNumber="100"
                app:textSize="10sp"
                >
    </com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>

    </LinearLayout>

    </LinearLayout>

  </LinearLayout>

</android.support.v7.widget.CardView>

2 个答案:

答案 0 :(得分:1)

您尚未为imageview指定ID

   <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:scaleType="fitXY"
            android:id="@+id/img_product"/>

和onCreateViewHolder如下所示

 View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_item_layout,parent,false);
    return new CartViewHolder(itemView);

答案 1 :(得分:0)

检查链接的网址无效或为空

String link=cartList.get(position).link
if(link != null){
Picasso.with(context)
        .load(link)
       .into(holder.img_product);
}