У меня есть некоторые проблемы с корзиной покупок, которые я записал по одной.
1. Когда я хочу увеличить количество товаров, счетчик товаров является общим для всех товаров в корзине Это означает, что если первый элемент равен 3, следующий элемент будет начинаться с 4. В чем проблема?
2. Он также записывает цены на последний элемент для всех.
3. когда я подтверждаю окончательную покупку, как очистить корзину?
4. И как рассчитать общую стоимость продуктов?
адаптер карты (FacultyAdapter)
dbhandler mydb;
int a=0;
int counter=1;
int baseprice=1;
private Context context;
private String mes="";
private List<Faculty> facultyList;
public FacultyAdapter(Context context, List<Faculty> facultyList) {
this.context = context;
this.facultyList=facultyList;
}
@NonNull
@Override
public FacultyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.itemcard,parent,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new FacultyViewHolder(view);
}
/////////////////////////////////////////////////
@Override
public void onBindViewHolder(@NonNull final FacultyViewHolder holder, int position) {
final Faculty s = facultyList.get(position);
holder.title.setText(s.getName());
holder.price.setText(s.getPrice());
baseprice=Integer.parseInt(s.getPrice());
String photoname=s.getImage();
Glide
.with(context)
.load( Urls.image+photoname)
.placeholder(R.mipmap.ic_launcher_round)
.error(R.drawable.logo)
.into(holder.img);
mydb=new dbhandler(context);
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String e=s.getId().toString();
mydb.delete(e);
notifyDataSetChanged();
}
});
кнопка увеличения и уменьшения
holder.inc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tprice=0;
Increment();
holder.count.setText(""+counter);
tprice= baseprice * counter;
holder.price.setText(""+tprice);
}
});
holder.dec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tprice=0;
Deccrement();
holder.count.setText(""+counter);
tprice= baseprice * counter;
holder.price.setText(""+tprice);
}
});
}
////////////////////////////////////////
@Override
public int getItemCount() {
return facultyList.size();
}
////////////////////////////////
public class FacultyViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView price,count;
CircleImageView img;
CardView card;
ImageButton remove,inc,dec;
private FacultyViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.txt_list_item_title);
price = itemView.findViewById(R.id.txt_list_item_des);
remove = itemView.findViewById(R.id.remove_item);
img=itemView.findViewById(R.id.image_cart);
inc=itemView.findViewById(R.id.increment);
dec=itemView.findViewById(R.id.decrement);
count=itemView.findViewById(R.id.count);
card = (CardView) itemView.findViewById(R.id.card);
}
}
функция увеличения и уменьшения
private void Increment(){
counter++;
}
private void Deccrement(){
if (counter==1)
return;
else {
counter--;
}
}
}