计算项目清单的总价

时间:2014-10-24 08:38:39

标签: android

目前,我已在项目列表中显示各自价格和数量的项目,使用php脚本检索所有值。 现在我想计算所列项目的总价,但我不知道如何...我认为可以在TransactionActivity.java(公共视图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;
}

}

1 个答案:

答案 0 :(得分:1)

只要您拥有transactionItems对象,就可以将它们添加到您设置适配器的位置。

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