我正在餐馆应用程序想要显示订单的选定项目,每个项目的价格和每个项目的总价格(计数*价格)。我想添加卡片商品列表的总价格,并显示在Recycler View外面的文本视图中,这些视图将动态更改。
在每张卡片视图中,有两个按钮( - 和+)可更改每个项目的计数值,以便更改每个项目的总价格。
这是我的适配器类
public class MyListsToOrderAdapter extends
RecyclerView.Adapter<MyListsToOrderAdapter.MyHOldd>{
ArrayList<Element> element;
Context ctx;
TextView textView;
String c;
String db = "Elements";
String dbp = "Prices";
PriceDao priceDao;
ElementDao elementDao;
int count=0;
private LayoutInflater inflater;
float v1,v4,v5,v2,v3,v6;
float x1,x2,x3=0;
public MyListsToOrderAdapter( ArrayList<Element> element,Context ctx,TextView textView) {
this.textView=textView;
this.ctx = ctx;
this.element = element;
inflater = LayoutInflater.from(ctx);
}
@Override
public MyListsToOrderAdapter.MyHOldd onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_lists_to_order,parent,false);
MyHOldd holder = new MyHOldd(view,ctx,element);
return holder;
}
@Override
public void onBindViewHolder(final MyListsToOrderAdapter.MyHOldd holder, final int position) {
priceDao=setUpDBPrice();
String bs= element.get(position).getCode();
List<Price> priceList = priceDao.queryBuilder().where(PriceDao.Properties.ElCode.eq(bs)).list();
String pricev = priceList.get(0).getPriceValue();
// Calculating the total price of each
v1 = Float.parseFloat(pricev);
v2= Float.parseFloat(element.get(position).getCount());
v3=v1*v2;
final String res = String.valueOf(v3);
holder.listName.setText(element.get(position).getDescription());
holder.add.setImageResource(R.drawable.add);
holder.sub.setImageResource(R.drawable.minus);
holder.pricess.setText(pricev);
holder.totalPriceT.setText(res);
holder.status.setText(element.get(position).getCount());
if(v5==1)v5=v6;
else if(v5==2) v5=-v6;
else if(v5==3) v5=0;
else v5=v3;
v4=v4+v5;
String ch = String.valueOf(v4);
textView.setText("Total: "+ch);
}
@Override
public int getItemCount() {
return element.size();
}
class MyHOldd extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView listName;
public TextView pricess;
public TextView status;
public TextView totalPriceT;
public ImageView add;
public ImageView sub;
int count;
Context ctx;
ArrayList<Element> element = new ArrayList<>();
ElementDao elementDao = setupDbElement();
public MyHOldd(View itemView, Context ctx, ArrayList<Element> element) {
super(itemView);
this.ctx=ctx;
this.element=element;
listName = (TextView)itemView.findViewById(R.id.TitemName);
pricess = (TextView)itemView.findViewById(R.id.TitemPrice);
status = (TextView)itemView.findViewById(R.id.Tstatus);
totalPriceT = (TextView)itemView.findViewById(R.id.totalPrice);
add = (ImageView) itemView.findViewById(R.id.TplusSign);
sub = (ImageView) itemView.findViewById(R.id.TminusSign);
add.setOnClickListener(this);
sub.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Element element = this.element.get(position);
String bd= element.getCode();
List<Price> priceList = priceDao.queryBuilder().where(PriceDao.Properties.ElCode.eq(bd)).list();
String pricev = priceList.get(0).getPriceValue();
// Calculating the total price of each
v6 = Float.parseFloat(pricev);
x3=v6*count;
v5=0;
count= Integer.parseInt(element.getCount());
if(view.getId()==add.getId()) {
count = count+1;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=1;
} else if (view.getId()==sub.getId()) {
if(count==0) {
count = 0;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=3;
}
else {
count = count-1;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=2;
}
}
count= Integer.parseInt(element.getCount());
}
}
答案 0 :(得分:0)
@Habatamu 每次单击add或sub imageview时,都应更新totalSum的textview。在您的情况下,覆盖方法onclick似乎不会更新totalSum。
答案 1 :(得分:0)
它对我来说就像这样,我改变了notifyDataSetChanged();到notifyItemChanged(position);
这样我就可以获得总值(v4)并将其传递给文本视图,如图所示
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Element element = this.element.get(position);
String bd= element.getCode();
List<Price> priceList = priceDao.queryBuilder().where(PriceDao.Properties.ElCode.eq(bd)).list();
String pricev = priceList.get(0).getPriceValue();
// Calculating the total price of each
v6 = Float.parseFloat(pricev);
x3=v6*count;
v5=0;
count= Integer.parseInt(element.getCount());
if(view.getId()==add.getId()) {
count = count+1;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=1;
} else if (view.getId()==sub.getId()) {
if(count==0) {
count = 0;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=3;
}
else {
count = count-1;
c = String.valueOf(count);
element.setCount(c);
elementDao.update(element);
notifyDataSetChanged();
v5=2;
}
}
count= Integer.parseInt(element.getCount());
}
} /////////////////////////////// 这样我就可以得到总值(v4)并将其传递给文本视图,如图所示
@Override
public void onBindViewHolder(final MyListsToOrderAdapter.MyHOldd holder, final int position) {
priceDao=setUpDBPrice();
String bs= element.get(position).getCode();
List<Price> priceList = priceDao.queryBuilder().where(PriceDao.Properties.ElCode.eq(bs)).list();
String pricev = priceList.get(0).getPriceValue();
// Calculating the total price of each
v1 = Float.parseFloat(pricev);
v2= Float.parseFloat(element.get(position).getCount());
v3=v1*v2;
final String res = String.valueOf(v3);
holder.listName.setText(element.get(position).getDescription());
holder.add.setImageResource(R.drawable.add);
holder.sub.setImageResource(R.drawable.minus);
holder.pricess.setText(pricev);
holder.totalPriceT.setText(res);
holder.status.setText(element.get(position).getCount());
holder.addNote.setText("Add");
//计算所有商品的总价
if(v5==1)v5=v6;
else if(v5==3) v5=-v6;
else if(v5==2) v5=0;
else v5=v3;
v4=v4+v5;
//显示在textView上
String ch = String.valueOf(v4);
textView.setText("Total: "+ch);
}