gpt4 book ai didi

java - 如何将Map值添加到ArrayList中?

转载 作者:行者123 更新时间:2023-12-02 04:10:29 25 4
gpt4 key购买 nike

I want to add map values into an arraylist.This is my current code.

  ArrayList<Map<String, String>> itemData = new ArrayList<>();
int index =0;

Map<String, String> itemselected = new HashMap<>();
itemselected.put("invoiceNo", textViewInvNo.getText().toString());//d
itemselected.put("cstName", textViewCstName.getText().toString());//d
itemselected.put("contact", textViewCstph.getText().toString());//d
itemselected.put("barcode", barCode.getText().toString());//d
itemselected.put("desc", itemDesc.getText().toString());//d
itemselected.put("weight", weightLine.getText().toString());//d
itemselected.put("rate", rateAmount.getText().toString());//d
itemselected.put("makingAmt", makingAmount.getText().toString());//d
itemselected.put("net_rate", netRate.getText().toString());//d
itemselected.put("itemTotal", itemtotal.getText().toString());//d
itemselected.put("vat", textViewVat.getText().toString());//d
itemselected.put("sum_total", textViewSum.getText().toString());//d
itemselected.put("bill_type", billType);//null

itemData.add(index,itemselected);
index++;

This is the output that i get.

[{rate=24000,weight=21.000,desc=item description,makingAmt=200,cstName=Test customer,sum_total=52094.46,vat=1021.46,itemTotal=51073,barcode=BQSP78BB,net_rate=24200,invoiceNo=1842,bill_type=estimate,contact=+91-8600931386}]

This is the output I am expecting.

[{rate=24000,weight=21.000,desc=item description,makingAmt=200,cstName=Test customer,sum_total=52094.46,vat=1021.46,itemTotal=51073,barcode=BQSP78BB,net_rate=24200,invoiceNo=1842,bill_type=estimate,contact=+91-8600931386},{rate=24000,weight=21.000,desc=item description,makingAmt=200,cstName=Test customer,sum_total=52094.46,vat=1021.46,itemTotal=51073,barcode=BQSP79BB,net_rate=24200,invoiceNo=1842,bill_type=estimate,contact=+91-8600931386}, {rate=24000,weight=21.000,desc=item description,makingAmt=200,cstName=Test customer,sum_total=52094.46,vat=1021.46,itemTotal=51073,barcode=BQSP80BB,net_rate=24200,invoiceNo=1842,bill_type=estimate,contact=+91-8600931386}]

I have a method called showItem() that contains itemTable().This is code for itemTable()

    private void itemTable(String itembarcode, String itemdesc, String weight, String rate, String making, String netrate, String total) {
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.setInputType(InputType.TYPE_CLASS_PHONE);
//rateAmount.setSelection(rateAmount.getText().length());
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.setInputType(InputType.TYPE_CLASS_PHONE);
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.setTag(count);
itemtotal.setLayoutParams(rowParams);
itemtotal.setGravity(Gravity.CENTER);
Toast.makeText(AddInvEst.this, "item count is " + itemtotal.getTag(), Toast.LENGTH_SHORT).show();
totals.add(total);
itemtotal.addTextChangedListener(totalTextWatcher);

double[] doubleList = new double[totals.size()];
double sum = 0.0d;
for (int i = 0; i < totals.size(); ++i) {
doubleList[i] = Double.parseDouble(totals.get(i));
sum += doubleList[i];
}

barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(netrate);
itemtotal.setText(total);
textViewSum.setText(sum * (0.02) + sum + "");//set total text to sum
textViewVat.setText(sum * (0.02) + "");

ArrayList<Map<String, String>> itemData = new ArrayList<>();
int index =0;

Map<String, String> itemselected = new HashMap<>();
itemselected.put("invoiceNo", textViewInvNo.getText().toString());//d
itemselected.put("cstName", textViewCstName.getText().toString());//d
itemselected.put("contact", textViewCstph.getText().toString());//d
itemselected.put("barcode", barCode.getText().toString());//d
itemselected.put("desc", itemDesc.getText().toString());//d
itemselected.put("weight", weightLine.getText().toString());//d
itemselected.put("rate", rateAmount.getText().toString());//d
itemselected.put("makingAmt", makingAmount.getText().toString());//d
itemselected.put("net_rate", netRate.getText().toString());//d
itemselected.put("itemTotal", itemtotal.getText().toString());//d
itemselected.put("vat", textViewVat.getText().toString());//d
itemselected.put("sum_total", textViewSum.getText().toString());//d
itemselected.put("bill_type", billType);//null

itemData.add(index,itemselected);
index++;




/*JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
try {
Toast.makeText(AddInvEst.this, "here the count is : "+count, Toast.LENGTH_SHORT).show();

jsonArray.put(count-1 ,itemselected);
jsonObject.put("itemSelected", jsonArray);

} catch (JSONException e) {
e.printStackTrace();
}*/

newRow.addView(barCode);
newRow.addView(itemDesc);
newRow.addView(weightLine);
newRow.addView(rateAmount);
newRow.addView(makingAmount);
newRow.addView(netRate);
newRow.addView(itemtotal);
itemTable.addView(newRow);

TextView display = (TextView) findViewById(R.id.tv_display);
display.setText(itemData.toString());

Toast.makeText(AddInvEst.this, itemData.toString(), Toast.LENGTH_SHORT).show();


}

This code for showItem()

    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();
}
//dynamic table generation code

itemTable(itembarcode, itemdesc, weight, rate, making, netrate, total);
}

This is where I call showItem in the else block.

   private void sendBarcode(final String url, final String barcodeNo) {
final Context context = getApplicationContext();
//String url = ITEM_URL+barcodeNum+toString().trim();
if (barcodeNo.equals("")) {
Toast.makeText(AddInvEst.this, "Please enter a valid barcode number ", Toast.LENGTH_LONG).show();
}
loading = ProgressDialog.show(AddInvEst.this, "Please wait...", "Fetching...", false, false);

StringRequest stringReq = new StringRequest(url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equalsIgnoreCase("notfound")) {
final Dialog dialogCrash = new Dialog(AddInvEst.this);
dialogCrash.setContentView(R.layout.crashreport);
TextView crashreport = (TextView) dialogCrash.findViewById(R.id.tv_crashcode);
crashreport.setText("Please Enter a valid Barcode Number ...");

Button bt_crash = (Button) dialogCrash.findViewById(R.id.bt_crash);
bt_crash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogCrash.dismiss();
}
});

dialogCrash.show();
loading.dismiss();

} else {

loading.dismiss();
showItem(response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Your Internet connection is slow or not available!", Toast.LENGTH_SHORT).show();
loading.dismiss();
}
});
RequestQueue reqQueue = Volley.newRequestQueue(context);
reqQueue.add(stringReq);
}

And sendBarcode is what I call onClick of a button.

最佳答案

class A {
List<Map<String,String>> itemData;

public A() {
itemData = new ArrayList<>();
}

private void itemTable {
...
int index =0;

Map<String, String> itemselected = new HashMap<>();
itemselected.put("invoiceNo", textViewInvNo.getText().toString());//d
itemselected.put("cstName", textViewCstName.getText().toString());//d
itemselected.put("contact", textViewCstph.getText().toString());//d
itemselected.put("barcode", barCode.getText().toString());//d
itemselected.put("desc", itemDesc.getText().toString());//d
itemselected.put("weight", weightLine.getText().toString());//d
itemselected.put("rate", rateAmount.getText().toString());//d
itemselected.put("makingAmt", makingAmount.getText().toString());//d
itemselected.put("net_rate", netRate.getText().toString());//d
itemselected.put("itemTotal", itemtotal.getText().toString());//d
itemselected.put("vat", textViewVat.getText().toString());//d
itemselected.put("sum_total", textViewSum.getText().toString());//d
itemselected.put("bill_type", billType);//null

itemData.add(index,itemselected);
index++;

...
}
}

关于java - 如何将Map值添加到ArrayList中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com