Reputation: 33
Currently I have displayed the items with their respective price and quantity in an item list, values all retrieved using php script. Now I want to calculate the total price of the listed items, but I have no idea how to.. I think it might be done at TransactionActivity.java (public View getView) as the respective prices are displayed there
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;
}
}
Upvotes: 1
Views: 5086
Reputation: 505
Just add them up where ever you're setting up your adapter once you have the transactionItems object.
double total=0;
for (TransactionItem item : transactionItems) {
total+=item.getPrice();
}
Log.d(TAG,"Total is:"+total);
Upvotes: 1