iamRaja iamRaja - 3 months ago 6
Android Question

How to pass three string from for loop

I have made an program to get 'item','qty','rate' now i have them all three in an string array but i need them to upload it in MYSQL but the values are reapeating several times in database.

Here is my code:

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;
}


I have made mistake in the looping process someone pls help me.

Answer

Assuming that items, qtys and rates have same 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);
    }
}