如何在同一个onclick方法中使用notifyDataSetChanged和setText

时间:2017-12-25 15:05:17

标签: android android-recyclerview recycler-adapter

我需要将notifyDataSetChanged()setText值用于onBindViewHolder内的TextView。我尝试在onClick方法中使用

qtyIncrease.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            quantityText.setText("Good");
            toatlPriceText.setText("250");
            notifyDataSetChanged();
        }
    });

我的适配器类,

public class CartCardviewAdapter extends RecyclerView.Adapter<CartCardviewAdapter.MyViewHolder> {

private Context context;
private ArrayList<CartCardviewModel> dataSet;
private ShoppingCart shoppingCart;

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView itemNameText, itemTypeText, itemDescText, unitPriceText, toatlPriceText, quantityText;
    ImageView itemImage, removeIcon, qtyIncrease, qtyDecrease;

    public MyViewHolder(final View itemView) {
        super(itemView);

        this.itemNameText = (TextView) itemView.findViewById(R.id.item_name_text);
        this.itemTypeText = (TextView) itemView.findViewById(R.id.item_type_text);
        this.itemDescText = (TextView) itemView.findViewById(R.id.item_desc_text);
        this.unitPriceText = (TextView) itemView.findViewById(R.id.item_unit_price_text);
        this.toatlPriceText = (TextView) itemView.findViewById(R.id.item_total_price_text);
        this.quantityText = (TextView) itemView.findViewById(R.id.item_quantity_text);

        this.itemImage = (ImageView) itemView.findViewById(R.id.item_image);
        this.removeIcon = (ImageView) itemView.findViewById(R.id.item_remove_icon);
        this.qtyIncrease = (ImageView) itemView.findViewById(R.id.cart_quantity_increase);
        this.qtyDecrease = (ImageView) itemView.findViewById(R.id.cart_quantity_decrease);
    }
}

public CartCardviewAdapter(ArrayList<CartCardviewModel> data) {
    this.dataSet = data;
}

@Override
public CartCardviewAdapter.MyViewHolder onCreateViewHolder(final ViewGroup parent,
                                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cart_cardview_layout, parent, false);

    CartCardviewAdapter.MyViewHolder myViewHolder = new CartCardviewAdapter.MyViewHolder(view);

    context = parent.getContext();
    return myViewHolder;
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBindViewHolder(final CartCardviewAdapter.MyViewHolder holder, final int listPosition) {

    shoppingCart = MainActivity.getCartInstance().getCart();

    TextView itemNameText = holder.itemNameText;
    TextView itemTypeText = holder.itemTypeText;
    TextView itemDescText = holder.itemDescText;
    TextView unitPriceText = holder.unitPriceText;
    final TextView toatlPriceText = holder.toatlPriceText;
    final TextView quantityText = holder.quantityText;

    ImageView itemImage = holder.itemImage;
    ImageView removeIcon = holder.removeIcon;
    ImageView qtyIncrease = holder.qtyIncrease;
    ImageView qtyDecrease = holder.qtyDecrease;

    itemNameText.setText(dataSet.get(listPosition).getItemName());
    itemTypeText.setText(dataSet.get(listPosition).getItemType());
    itemDescText.setText(dataSet.get(listPosition).getItemDescription());
    unitPriceText.setText(String.valueOf(dataSet.get(listPosition).getUnitPrice()));
    toatlPriceText.setText(String.valueOf(dataSet.get(listPosition).getTotalPrice()));

    String qty;
    if (dataSet.get(listPosition).getItemQuantity() < 9) {
        qty = "0" + String.valueOf(dataSet.get(listPosition).getItemQuantity());
    } else {
        qty = String.valueOf(dataSet.get(listPosition).getItemQuantity());
    }
    quantityText.setText(qty);

    Picasso.with(context).load(dataSet.get(listPosition).getItemImageUrl()).into(itemImage);

    removeIcon.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(v.getContext(), String.valueOf(listPosition), Toast.LENGTH_SHORT).show();
            shoppingCart.removeItem(listPosition);
            dataSet.remove(listPosition);
            notifyDataSetChanged();
        }
    });

    qtyIncrease.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            shoppingCart.increaseItemQuantity(listPosition);
            quantityText.setText(String.valueOf(shoppingCart.getItemQuantity(listPosition)));
            toatlPriceText.setText(String.valueOf(shoppingCart.getTotalPrice(listPosition)));
            notifyDataSetChanged();
        }
    });

    qtyDecrease.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (shoppingCart.getItemQuantity(listPosition) == 1) {
                Toast.makeText(v.getContext(), "Already Minimum Quantity is Reached", Toast.LENGTH_SHORT).show();
            } else {
                shoppingCart.decreaseItemQuantity(listPosition);
                quantityText.setText(String.valueOf(shoppingCart.getItemQuantity(listPosition)));
                toatlPriceText.setText(String.valueOf(shoppingCart.getTotalPrice(listPosition)));
                notifyDataSetChanged();
            }
        }
    });

}

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

但两者都无法点击。我需要回去看看受影响的变化。我无法理解这个问题。请帮帮我

2 个答案:

答案 0 :(得分:0)

首先此行@RequiresApi(api = Build.VERSION_CODES.N)限制性太强,请考虑较低级别的行。有很多方法可以解决这个问题,首先你可以使用RecyclerView.OnItemTouchListener来托管适配器的回收站。以下是描述单击和长按Link的详细教程。另一种方法是界面分割原则。

  1. 在适配器类中,使用一种方法
  2. 创建一个接口
  3. 方法签名(视图视图,int位置),描述了在适配器中单击了相应位置的视图。
  4. 在适配器
  5. 中声明此接口的实例
  6. 修改构造函数以接受该类型的接口
  7. 让具有recylcler视图实例的类实现上面的接口
  8. 将新实例传递给已实现类
  9. 中的构造函数
  10. 然后在覆盖方法中,更改您的文字并致电adaptor.notifyItemChanged(position)
  11. 哦,哦,在视图获取init的适配器类中,使用接口实例设置onClicklistener。
  12. 本教程详细描述了上述内容link

    欢迎你!

答案 1 :(得分:-1)

点击不会从那里触发,你需要在ViewHolder中设置图像视图“qtyIncrease”的点击监听器而不是“OnBindViewHolder”

  public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView itemNameText, itemTypeText, itemDescText, unitPriceText, toatlPriceText, quantityText;
    ImageView itemImage, removeIcon, qtyIncrease, qtyDecrease;

    public MyViewHolder(final View itemView) {
        super(itemView);

        this.itemNameText = (TextView) itemView.findViewById(R.id.item_name_text);
        this.itemTypeText = (TextView) itemView.findViewById(R.id.item_type_text);
        this.itemDescText = (TextView) itemView.findViewById(R.id.item_desc_text);
        this.unitPriceText = (TextView) itemView.findViewById(R.id.item_unit_price_text);
        this.toatlPriceText = (TextView) itemView.findViewById(R.id.item_total_price_text);
        this.quantityText = (TextView) itemView.findViewById(R.id.item_quantity_text);

        this.itemImage = (ImageView) itemView.findViewById(R.id.item_image);
        this.removeIcon = (ImageView) itemView.findViewById(R.id.item_remove_icon);
        this.qtyIncrease = (ImageView) itemView.findViewById(R.id.cart_quantity_increase);
        this.qtyDecrease = (ImageView) itemView.findViewById(R.id.cart_quantity_decrease);


        qtyIncrease.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            quantityText.setText("Good");
            toatlPriceText.setText("250");
            notifyDataSetChanged();
        }
    });

}