gpt4 book ai didi

android - 计算项目列表的总价

转载 作者:行者123 更新时间:2023-11-30 02:35:59 26 4
gpt4 key购买 nike

目前我已经在项目列表中显示了项目及其各自的价格和数量,所有值都是使用 php 脚本检索的。 现在我想计算所列商品的总价,但我不知道如何......我认为这可能是在 TransactionActivity.java(公共(public) View getView)中完成的,因为显示了各自的价格那里

TransactionActivity.java

private void setDetails() {
TransactionItemListAdapter listAdapter = new TransactionItemListAdapter(this, R.layout.list_transaction_item, transaction.getItems());
setListAdapter(listAdapter);
}
private class TransactionItemListAdapter extends ArrayAdapter<TransactionItem>
implements OnClickListener{
private LayoutInflater mInflater = null;

public TransactionItemListAdapter(Context context, int resource,
ArrayList<TransactionItem> items) {
super(context, resource, items);
mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.list_transaction_item, null);
} else {
view = convertView;
}

TextView textName = (TextView) view.findViewById(R.id.name);
TextView textPrice = (TextView) view.findViewById(R.id.price);
TextView textQty = (TextView) view.findViewById(R.id.qty);

TransactionItem item = getItem(position);
textName.setText(item.getName());
textQty.setText(String.valueOf(item.getQty()));
textPrice.setText(String.format(priceFormat, item.getPrice()));


view.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
// list selection is disabled
}
}

TransactionItem.java

public class TransactionItem {

private String stockName=null;
private int quantity=0;
private double price =0;
private double totalprice=0;
public TransactionItem(String stockName, int quantity, double totalprice, double price)
{
this.stockName=stockName;
this.quantity=quantity;
this.totalprice=totalprice;
this.price=price;
}

public String getName(){
return stockName;
}
public int getQty() {
return quantity;
}

public double getPrice() {
return price;
}
public double getSubtotal() {
return totalprice;
}

}

最佳答案

一旦您拥有 transactionItems 对象,只需将它们添加到您设置适配器的位置即可。

double total=0;
for (TransactionItem item : transactionItems) {
total+=item.getPrice();
}
Log.d(TAG,"Total is:"+total);

关于android - 计算项目列表的总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26544313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com