如何从for循环中传递三个字符串

时间:2016-09-07 14:52:25

标签: android arrays string for-loop

我已经制作了一个程序来获取'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;
    }

我在循环过程中犯了错误,有人请帮助我。

1 个答案:

答案 0 :(得分:2)

假设itemsqtysrates具有相同的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);
    }
}