如何在TextView中显示List中可用的所有项目的总计

时间:2014-08-29 05:20:17

标签: java android

我正在撰写food ordering计划,并且在CartActivity我必须显示Grand Total中所有items Cart的{​​{1}}。

How我的CartActivity现在看来是这样的:

enter image description here

尽可能see我正在使用TextView ListView 0.00 default value作为Total,我想在此显示list items所有Can someone help me ? ....

believe因为我you许多experienced已经public class CartActivity extends Activity{ ListView listView; CartAdapter cartAdapter; TextView textGrandTotal; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_cart); listView = (ListView) findViewById(R.id.listViewCart); textGrandTotal = (TextView) findViewById(R.id.textGrandTotal); cartAdapter = new CartAdapter(CartActivity.this); listView.setAdapter(cartAdapter); if(Handler.itemsHandler.size()>0) { for (int i = 0; i < Handler.itemsHandler.size(); i++) { } } } } 这个

CartActivity.java: -

public class CartAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public CartAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return Handler.itemsHandler.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

    // Declare Variables

    public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables

    final ViewHolder holder;
    if(convertView==null)
    {
        holder = new ViewHolder();
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.adapter_cart, null);

        // Locate the TextViews in listview_item.xml
        holder.title = (TextView) convertView.findViewById(R.id.textTitle);
        holder.cost = (TextView) convertView.findViewById(R.id.textCost);
        holder.total = (TextView) convertView.findViewById(R.id.textTotal);
        holder.quantity = (TextView) convertView.findViewById(R.id.editQuantity);
        holder.add = (ImageButton) convertView.findViewById(R.id.btnAdd);
        holder.less = (ImageButton) convertView.findViewById(R.id.btnLess);

        holder.quantity.setText("1");

        holder.quantity.setEnabled(false);

        HashMap<String, String> item = new HashMap<String, String>();
        item = Handler.itemsHandler.get(position);

        // Capture position and set results to the TextViews
        holder.title.setText(item.get(ItemActivity.OBJECT_TITLE));
        holder.cost.setText(item.get(ItemActivity.OBJECT_COST));
        Log.d("Getting Handler", "Item [getting]:: " + item);

        String strValue = holder.quantity.getText().toString();
        Log.d("quantity::", strValue);
        int newValue = Integer.parseInt(strValue);
        Log.d("newValue", String.valueOf(newValue));
        String strCost = holder.cost.getText().toString();
        Log.d("cost::", strCost);
        double costValue = Double.parseDouble(strCost);
        Log.d("double::cost:-", String.valueOf(costValue));
        holder.total.setText(new DecimalFormat("##.#").format(costValue*newValue));
        String total = holder.total.getText().toString();
        Log.d("total::", total);

        ..........

        return convertView;
    }
}

CartAdapter.java: -

public class Handler {

    public static ArrayList<HashMap<String, String>> itemsHandler = new ArrayList<HashMap<String, String>>();

}

Handler.java: -

{{1}}

1 个答案:

答案 0 :(得分:0)

public class CartActivity extends Activity{
    ListView listView;
    CartAdapter cartAdapter;
    TextView textGrandTotal;
     double sum=0;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cart);

        listView = (ListView) findViewById(R.id.listViewCart);
        textGrandTotal = (TextView) findViewById(R.id.textGrandTotal);
        cartAdapter = new CartAdapter(CartActivity.this);
        listView.setAdapter(cartAdapter);

        if(Handler.itemsHandler.size()>0)
        {
            for (int i = 0; i < Handler.itemsHandler.size(); i++) {
                 HashMap<String, String> item = new HashMap<String, String>();
                 item = Handler.itemsHandler.get(position);
                String strCost= item.get(ItemActivity.OBJECT_COST);
                //add this line of code
                String quantity= item.get(ItemActivity.OBJECT_QUANTITY);
                double costValue = Double.parseDouble(strCost*quantity);
                 sum=sum+costValue;

            }           
        }
    }

}

你必须像我上面那样乘以(每件物品的成本*物品数量)。