我有一个字符串arraylist,其数字如[“51,073”,“51,074”,“51,073”]现在我想将其转换为Double ArrayList并计算数组列表中数字的总和。
private ArrayList<String> totals = new ArrayList<>();
private ArrayList<Double> demototal = new ArrayList<>();
String total = itemData.getString(ParseBarcode.KEY_TOTAL);
totals.add(total);
/*converting all values from totals array to Double and add to arraylist demototal*/
for (int i = 0; i < totals.size(); i++) {
final String value = totals.get(i);
double total_ary = (double)Math.round((Double.parseDouble(value))*100.0)/100.0;
demototal.add(total_ary);
}
String amount = Double.toString(setArrayListElement(demototal));
textViewSum.setText(amount);//set total text to amount
//calculate amount here we pass setArrayListElement as Double arraylist
private Double setArrayListElement(ArrayList inArray) {
Double amount = 0.0d;
for (int i = 0; i < inArray.size(); i++) {
amount += Double.valueOf(Math.round((Double) inArray.get(i))*100.0)/100.0;
}
return amount;
}
为了更清楚,我正在添加更多代码。请帮忙!
private void showItem(String json) {
String itembarcode = "";
String itemdesc = "";
String weight = "";
String rate = "";
String making = "";
String netrate = "";
String total = "";
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(ParseBarcode.JSON_ARRAY);
JSONObject itemData = result.getJSONObject(0);
itembarcode = itemData.getString(ParseBarcode.KEY_BARCODE);
itemdesc = itemData.getString(ParseBarcode.KEY_DESC);
weight = itemData.getString(ParseBarcode.KEY_WEIGHT);
rate = itemData.getString(ParseBarcode.KEY_RATE);
making = itemData.getString(ParseBarcode.KEY_MAKING);
netrate = itemData.getString(ParseBarcode.KEY_NETRATE);
total = itemData.getString(ParseBarcode.KEY_TOTAL);
} catch (JSONException e) {
e.printStackTrace();
}
//table started
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
rowParams.setMargins(16, 0, 16, 0);
TableLayout tableLayout = new TableLayout(AddInvEst.this);
tableLayout.setLayoutParams(tableParams);
TableRow newRow = new TableRow(AddInvEst.this);
newRow.setLayoutParams(tableParams);
barCode = new TextView(AddInvEst.this);
barCode.setLayoutParams(rowParams);
barCode.setGravity(Gravity.CENTER);
itemDesc = new TextView(AddInvEst.this);
itemDesc.setLayoutParams(rowParams);
itemDesc.setGravity(Gravity.CENTER);
weightLine = new TextView(AddInvEst.this);
weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));
weightLine.setGravity(Gravity.CENTER);
rateAmount = new EditText(AddInvEst.this);
rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
rateAmount.setGravity(Gravity.CENTER);
rateAmount.addTextChangedListener(rateTextWatcher);
makingAmount = new EditText(AddInvEst.this);
makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
makingAmount.setGravity(Gravity.CENTER);
makingAmount.addTextChangedListener(mkAmountTextWatcher);
netRate = new TextView(AddInvEst.this);
netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
netRate.setGravity(Gravity.CENTER);
netrates.add(netrate);
itemtotal = new TextView(AddInvEst.this);
itemtotal.setLayoutParams(rowParams);
itemtotal.setGravity(Gravity.CENTER);
totals.add(total);
//converting all values from totals array to Double and add to arraylist demototal
for (int i = 0; i < totals.size(); i++) {
final String value = totals.get(i);
double total_ary = (double)Math.round((Double.parseDouble(value))*100.0)/100.0;
demototal.add(total_ary);
}
barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(netrate);
itemtotal.setText(total);
String amount = Double.toString(setArrayListElement(demototal));
textViewSum.setText(amount);//set total text to amount
//textViewVat.setText(vatAmt);
newRow.addView(barCode);
newRow.addView(itemDesc);
newRow.addView(weightLine);
newRow.addView(rateAmount);
newRow.addView(makingAmount);
newRow.addView(netRate);
newRow.addView(itemtotal);
itemTable.addView(newRow);
}
//calculate amount here we pass setArrayListElement as Double arraylist
private Double setArrayListElement(ArrayList inArray) {
Double amount = 0.0d;
for (int i = 0; i < inArray.size(); i++) {
amount += Double.valueOf(Math.round((Double) inArray.get(i))*100.0)/100.0;
}
return amount;
}
答案 0 :(得分:0)
您向demototal
添加元素,但从不删除它们或擦除列表。这意味着它将永远保持增长。