我正在开发购物车,我想增加当用户单击recyclerview的每一行中的按钮时购买的商品的数量。
当用户单击“增加”按钮时,我试图增加编辑文本,但是它会更新recyclerview中所有编辑文本的值。
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="Add"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textColor="#FFF" />
<EditText
android:layout_weight="1"
android:id="@+id/numberOfItems"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/add"
android:inputType="phone" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/numberOfItems"
android:text="+"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dc3434"
android:text="Remove"
android:textColor="#FFF" />
</LinearLayout>
适配器
public class BreadAdapter extends RecyclerView.Adapter<BreadAdapter.ViewHolder> {
public List<BreadModel> breads;
Context context;
private int quntity= 0;
public BreadAdapter(List<BreadModel> breads, Context context) {
this.breads = breads;
this.context = context;
}
@NonNull
@Override
public BreadAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_bread_recyclerview, parent, false));
}
@Override
public void onBindViewHolder(@NonNull final BreadAdapter.ViewHolder holder, int position) {
final BreadModel bread_title = breads.get(position);
holder.tv_title.setText(bread_title.getName());
holder.tv_price.setText(String.valueOf(bread_title.getPrice()));
Log.i("info", bread_title.getImg());
GlideApp.with(context)
.load(bread_title.getImg())
holder.increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
quntity= quntity+ 1;
holder.et_numberOfItems.setText(String.valueOf(quntity));
}
});
}
public void update(){
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return breads.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final EditText et_numberOfItems;
TextView tv_title;
TextView tv_price;
ImageView img_breads;
CardView cv_bread;
AppCompatButton add;
AppCompatButton remove;
AppCompatButton increase;
AppCompatButton decrease;
public ViewHolder(View itemView) {
super(itemView);
et_numberOfItems = (EditText) itemView.findViewById(R.id.numberOfItems);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_price = (TextView) itemView.findViewById(R.id.tv_price);
img_breads = (ImageView) itemView.findViewById(R.id.breadImageViews);
cv_bread = (CardView) itemView.findViewById(R.id.breadCardView);
add = (AppCompatButton) itemView.findViewById(R.id.add);
remove = (AppCompatButton) itemView.findViewById(R.id.remove);
increase = (AppCompatButton) itemView.findViewById(R.id.increase);
decrease = (AppCompatButton) itemView.findViewById(R.id.decrease);
}
}
}
这是我的适配器代码,请仔细阅读并帮助我。
这是屏幕截图 如果我点击加号(+)按钮,则会增加所有edittext上的项目数,而不是该特定项目