2014-08-29 82 views
0

可用的所有項目的總TextView的我寫food ordering程序,並在CartActivity我都必須表現Cart所有可用itemsGrand Total如何顯示在列表

這裏是HowCartActivity看起來現在:

enter image description here

正如你可以see我使用的是TextView以上ListView,其中有0.00default value,在這裏我想告訴所有list itemsTotal。 ...

Can someone help me ?因爲我believe許多you已經experienced

CartActivity.java:-

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++) { 
      }   
     } 
    } 

} 

CartAdapter.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; 
    } 
} 

Handler.java:-

public class Handler { 

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

} 
+1

你正在計算每個項目的總成本r8將他們添加到一個數組列表,總結他們使用for循環設置他們在文本視圖 – 2014-08-29 05:29:41

+0

名稱選擇「Hanlder」是有趣的:)肯定不推薦 – user210504 2014-08-29 05:37:28

+0

samosa蔬菜pakora ........ mmmm,美味的代碼 – nobalG 2014-08-29 05:39:48

回答

0
​​

你只需要乘以(每個項目的成本*項目數量)像上面我一樣。

+0

你是做總項目成本...但我問如何計算總計 - 當我有項目(S)的總數...更清晰看到上面的圖像屏幕快照 – Mysterious 2014-08-29 08:49:22

+0

我想你只是簡單地添加一行代碼。往上看。 – 2014-08-29 13:27:48