可用的所有項目的總TextView的我寫food ordering
程序,並在CartActivity
我都必須表現Cart
所有可用items
的Grand Total
。如何顯示在列表
這裏是How
我CartActivity
看起來現在:
正如你可以see
我使用的是TextView
以上ListView
,其中有0.00
爲default value
,在這裏我想告訴所有list items
的Total
。 ...
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>>();
}
你正在計算每個項目的總成本r8將他們添加到一個數組列表,總結他們使用for循環設置他們在文本視圖 – 2014-08-29 05:29:41
名稱選擇「Hanlder」是有趣的:)肯定不推薦 – user210504 2014-08-29 05:37:28
samosa蔬菜pakora ........ mmmm,美味的代碼 – nobalG 2014-08-29 05:39:48