我已经制作了一个程序来获取'item','qty','rate'现在我将它们全部放在一个字符串数组中,但是我需要它们在MYSQL中上传它们但是这些值在数据库中重复了几次。
这是我的代码:
String items[] = itemsF.split("\n");
String qtys[] = qtyV.split("\n")
String rates[] = rateF.split("\n");
for (final String r : rates) {
for (final String it : items) {
Toast.makeText(BillGenerating.this, it, Toast.LENGTH_SHORT).show();
for (final String qt : qtys) {
Toast.makeText(BillGenerating.this, qt, Toast.LENGTH_SHORT).show();
class AddBillDetails extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(BillGenerating.this, "Adding...", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(BillGenerating.this, s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... v) {
HashMap<String, String> params = new HashMap<>();
params.put(KEY_BILL_ITEMS, it);
params.put(KEY_BILL_QTY, qt);
params.put(KEY_BILL_RATE, r);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.BILL_DETAILS_LOCAL_JOB, params);
return res;
}
}
AddBillDetails ae = new AddBillDetails();
ae.execute();
}
}
}
return sum;
}
我在循环过程中犯了错误,有人请帮助我。
答案 0 :(得分:2)
假设items
,qtys
和rates
具有相同的length
。
String[] items = itemsF.split("\n");
String[] qtys = qtyV.split("\n");
String[] rates = rateF.split("\n");
for(int i = 0; i < items.length; i++){
AddBillDetails ae = new AddBillDetails();
ae.execute(items[i], qtys[i], rates[i]);
}
<强> AddBillDetails.class 强>
class AddBillDetails extends AsyncTask<String, String, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(BillGenerating.this, "Adding...", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(BillGenerating.this, s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... v) {
HashMap<String, String> params = new HashMap<>();
params.put(KEY_BILL_ITEMS, v[0]);
params.put(KEY_BILL_QTY, v[1]);
params.put(KEY_BILL_RATE, v[2]);
RequestHandler rh = new RequestHandler();
return rh.sendPostRequest(Config.BILL_DETAILS_LOCAL_JOB, params);
}
}