我在recyclerview中有两个按钮,用于数量值的递增(+)和减量(-),就像购物车应用程序一样,同时将第0个项目的项目值递增到我的下一个项目,即第1个项目的递增值等于第0个项目的先前值如果我的第0个项目的值为4,而对于第一个项目,如果我将其递增1,那么它将直接花费5,而不是从0开始。
public class TeaListAdapter extends RecyclerView.Adapter<TeaListAdapter.MyViewHolder> {
private int num=0;
private List<TeaListPOJO> teaItemList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvitemName, tvitemRate,number;
public ImageView ivItemImg;
public Button btnIncrease,btnDecrease;
RecyclerView.ViewHolder holder;
public MyViewHolder(View view) {
super(view);
tvitemName = (TextView) view.findViewById(R.id.txt_item_name);
tvitemRate = (TextView) view.findViewById(R.id.txt_item_price);
ivItemImg= (ImageView) view.findViewById (R.id.iv_item);
number=(TextView) view.findViewById (R.id.integer_number);
btnIncrease =view.findViewById(R.id.btn_increase);
btnDecrease = view.findViewById(R.id.btn_decrease);
}
}
public TeaListAdapter(List<TeaListPOJO> teaItemList) {
this.teaItemList = teaItemList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_placeorder_items, parent, false);
return new MyViewHolder (itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final TeaListPOJO tealist = teaItemList.get(position);
holder.tvitemName.setText(tealist.getItemName ());
holder.tvitemRate.setText(AppConstants.INDIAN_RUPEE_SIGN.concat (tealist.getItemRate ()));
// holder.number.setText (tealist.getQty ());
holder.btnIncrease.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
num++;
holder.number.setText (String.valueOf (num));
notifyDataSetChanged();
}
});
holder.btnDecrease.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
if(num>0)
num--;
holder.number.setText (String.valueOf (num));
}
});
byte[] decodedString = new byte[0];
try {
decodedString = Base64.decode(tealist.getImageStr(), Base64.DEFAULT);
// tenantModelPOJO.setLogo(decodedString);
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.ivItemImg.setImageBitmap(Bitmap.createScaledBitmap(bmp,60,
60, false));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return teaItemList.size();
}
}
答案 0 :(得分:1)
您的问题是对所有视图仅声明一个变量,您应该从TextView
中获取值,然后进行修改,然后将其重新设置为TextView
尝试此代码
holder.btnIncrease.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int val = 0;
try{
val = Integer.parseInt(holder.number.getText().toString());
}catch(Exception e) {
val = 0;
}
holder.number.setText (String.valueOf (val++));
}
});
holder.btnDecrease.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int val = 0;
try{
val = Integer.parseInt(holder.number.getText().toString());
}catch(Exception e) {
val = 0;
}
if(val>0) val--;
holder.number.setText (String.valueOf (val));
}
});