我有一个ListView
,其中包含3个TextViews
列,我可以点击其中一个,然后显示DialogAlert
并选择数量。
从AlertDialog
中选择数量时,我的特定产品价格会乘以数量。
现在,问题是我希望ListVIew
为所有产品制作Total
,我无法弄清楚如何计算产品总数当数量改变时。
My adapter :
public class VanzatorProduseList extends ArrayAdapter<VanzatorProduse> {
private Activity context;
private List<VanzatorProduse> vanzatorProduseList;
private double total = 0.0;
public VanzatorProduseList(Activity context, List<VanzatorProduse> vanzatorProduseList) {
super(context, R.layout.list_produse_vanzator, vanzatorProduseList);
this.context = context;
this.vanzatorProduseList = vanzatorProduseList;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_produse_vanzator, null, true);
final TextView textViewProdus1 = (TextView) listViewItem.findViewById(R.id.textViewProdus1);
final TextView textViewPret1 = (TextView) listViewItem.findViewById(R.id.textViewPret1);
final TextView textViewCantitate1 = (TextView) listViewItem.findViewById(R.id.textViewCantitate1);
final VanzatorProduse vanzatorProduse = vanzatorProduseList.get(position);
textViewProdus1.setText(vanzatorProduse.getProdus());
textViewPret1.setText(vanzatorProduse.getPret());
textViewCantitate1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = context.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_alert, null);
dialogBuilder.setView(dialogView);
final ElegantNumberButton elegantNumberButton = (ElegantNumberButton) dialogView.findViewById(R.id.elegantNumberButton);
final Button dialog_btn = (Button) dialogView.findViewById(R.id.dialog_btn);
dialogBuilder.setTitle("Alege cantitate : " + vanzatorProduse.getProdus());
final AlertDialog b = dialogBuilder.create();
b.show();
dialog_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num = elegantNumberButton.getNumber();
total = (Integer.parseInt(vanzatorProduse.getPret()) * (Integer.parseInt(num)));
textViewPret1.setText(String.valueOf(total));
textViewCantitate1.setText(num);
b.dismiss();
}
});
}
});
return listViewItem;
}
}
希望你能理解我的问题,我对这个问题确实有一些严重的问题,即使我找了很多教程,也没有发生任何事情。
答案 0 :(得分:1)
好的,首先,使用set / get方法在VanzatorProduse
类中添加一个新字段:
public class VanzatorProduse {
int subTotal;
public int getSubTotal(){
return subTotal;
}
public void setSubTotal(int subTotal){
this.subTotal = subTotal;
}
}
接下来,修改您的适配器:
public class VanzatorProduseList extends ArrayAdapter<VanzatorProduse> {
private Activity context;
private List<VanzatorProduse> vanzatorProduseList;
private double total = 0.0;
public VanzatorProduseList(Activity context, List<VanzatorProduse> vanzatorProduseList) {
super(context, R.layout.list_produse_vanzator, vanzatorProduseList);
this.context = context;
this.vanzatorProduseList = vanzatorProduseList;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_produse_vanzator, null, true);
final TextView textViewProdus1 = (TextView) listViewItem.findViewById(R.id.textViewProdus1);
final TextView textViewPret1 = (TextView) listViewItem.findViewById(R.id.textViewPret1);
final TextView textViewCantitate1 = (TextView) listViewItem.findViewById(R.id.textViewCantitate1);
final VanzatorProduse vanzatorProduse = vanzatorProduseList.get(position);
textViewProdus1.setText(vanzatorProduse.getProdus());
textViewPret1.setText(vanzatorProduse.getPret());
textViewCantitate1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = context.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_alert, null);
dialogBuilder.setView(dialogView);
final ElegantNumberButton elegantNumberButton = (ElegantNumberButton) dialogView.findViewById(R.id.elegantNumberButton);
final Button dialog_btn = (Button) dialogView.findViewById(R.id.dialog_btn);
dialogBuilder.setTitle("Alege cantitate : " + vanzatorProduse.getProdus());
final AlertDialog b = dialogBuilder.create();
b.show();
dialog_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num = elegantNumberButton.getNumber();
total = (Integer.parseInt(vanzatorProduse.getPret()) * (Integer.parseInt(num)));
textViewPret1.setText(String.valueOf(total));
textViewCantitate1.setText(num);
b.dismiss();
vanzatorProduse.setSubTotal(total);
Toast.makeText(context, "total : " + calculateTotal(), Toast.LENGTH_LONG).show();
}
});
}
});
return listViewItem;
}
public int calculateTotal(){
int total = 0;
for(VanzatorProduse vanzator: vanzatorProduseList){
total+= vanzator.getSubTotal();
}
return total;
}
}