【问题标题】:Handling click button in each RecyclerView and update TextView value处理每个 RecyclerView 中的单击按钮并更新 TextView 值
【发布时间】:2017-09-14 19:02:30
【问题描述】:

单击按钮时,我必须更新同一位置的 TextView,我已经完成了,但 RecyclerView 的第 9 和第 10 位置跟随第 1 位置和第 2 位置。换句话说,如果我单击第一个按钮位置,TextView 的第一个位置会更新,但是 TextView 的第 9 个位置也更新了,应该不会更新。如何解决?

我关注this link

这是我的适配器

class ProductsByStoreAdapter extends RecyclerView.Adapter<ProductsByStoreAdapter.ViewHolder> {

private ArrayList<Products> products;

 ProductsByStoreAdapter(ArrayList<Products> productses) {
     this.products = productses;
     //products = CenterRepository.getCenterRepository()
             //.getListOfProductsInShoppingList();
}

@Override
public ProductsByStoreAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.products_card_item, viewGroup, false);
    return new ProductsByStoreAdapter.ViewHolder(view);
}

class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_product_name, tv_product_price, tv_product_quantity;
    private ImageView im_product_image;
    private ImageButton button_add_product, button_min_product;
    private EditText e_note;
    private LinearLayout layout_note;
    ViewHolder(View view) {
        super(view);

        im_product_image            = (ImageView)view.findViewById(R.id.product_image);
        tv_product_name             = (TextView)view.findViewById(R.id.product_name);
        tv_product_price            = (TextView)view.findViewById(R.id.product_price);
        tv_product_quantity         = (TextView)view.findViewById(R.id.product_quantity);
        e_note                      = (EditText)view.findViewById(R.id.e_note);
        layout_note                 = (LinearLayout)view.findViewById(R.id.layout_note);

        this.button_add_product = (ImageButton)view.findViewById(R.id.button_add_product);
        button_min_product = (ImageButton)view.findViewById(R.id.button_min_product);

    }
}

@Override
public void onBindViewHolder(final ProductsByStoreAdapter.ViewHolder viewHolder, final int position) {
    Glide.with(viewHolder.im_product_image.getContext())
            .load(products.get(position).getImage_uri())
            .centerCrop()
            .crossFade()
            //.placeholder(R.drawable.placeholder_main)
            .into(viewHolder.im_product_image);
    CurrencyFormats currencyFormat = new CurrencyFormats();
    viewHolder.tv_product_name.setText(products.get(position).getName());
    viewHolder.tv_product_price.setText(currencyFormat.toRupiah(products.get(position).getPrice()));
    //viewHolder.tv_product_quantity.setText("0");
    viewHolder.button_add_product.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //current object
            Products tempObj = (products).get(position);
         ((ProductsByStoreActivity)view.getContext()).updateItemCount(true);
                tempObj.setQuantity(String.valueOf(1));
          viewHolder.tv_product_quantity.setText(tempObj.getQuantity());
        }
    });
    viewHolder.button_min_product.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Products tempObj = (products).get(position);
                                 viewHolder.tv_product_quantity.setText(CenterRepository
                            .getCenterRepository().getListOfProductsInShoppingList()
                            .get(indexOfTempInShopingList).getQuantity());
                   
                        ((ProductsByStoreActivity)view.getContext()).updateItemCount(false);
                    }
                }
            }else {

            }
        }
    });

}

@Override
public int getItemCount() {
    //return products.size();
    return products == null ? 0 : products.size();
}}

【问题讨论】:

  • 你有没有试过在改变 onClick 的值之后调用 notifyDataSetChanged ?
  • 我把 notifyDataSetChanged() 放在哪里?@AalapPatel
  • 在 onClick 中改变值之后
  • 你的意思是更新TextView值之后?

标签: android android-recyclerview


【解决方案1】:

您需要将 onclick 侦听器移动到 onCreateViewHolder

final ProductsByStoreAdapter.ViewHolder viewHolder = new ProductsByStoreAdapter.ViewHolder(view);
button_add_product.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        //current object
        Products tempObj = products.get(viewHolder.getAdapterPosition(););
     ((ProductsByStoreActivity)view.getContext()).updateItemCount(true);
            tempObj.setQuantity(String.valueOf(1));
      viewHolder.tv_product_quantity.setText(tempObj.getQuantity());
    }
});
return viewHolder;

你可以对其他 onclicklistener 做同样的事情

【讨论】:

    【解决方案2】:

    更新:除非单击按钮,否则您不会在 BindView 函数中将文本设置为您的 product_quantity 文本视图。这意味着它的价值将从其他物品中回收。您应该使用 if 语句检查项目的数量,即使不点击也可以显示它。

    旧的且不正确的答案: 我不确定这是否是问题所在,但它很容易检查,因此请尝试一下。有 2 个位置 - 适配器位置和布局位置。我认为您使用的位置(来自 onBind 函数的位置)可能是后者。您想要适配器位置,因此请尝试像这样使用 getAdapterPosition(): 产品 tempObj = (products).get(getAdapterPosition());

    【讨论】:

      【解决方案3】:

      添加下面的行来解决item的第9和第10个位置的问题

      @Override
      public int getItemViewType(int position) 
      {
          return position;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-12-26
        • 2017-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多