我试图从循环中调用 AsyncTask 并为我的 AsyncTask 更新类 varbile。
我的问题是programPosition不更新是真正的价值,只给我是
循环中的最后一个值。
观看我的 logcatprogramPosition在我的循环中得到更新,但我的 AsyncTask 得到了
只有最后一个值programPosition
感谢您的帮助。
这是我的代码和我的日志猫:
class CreateNewWorkout extends AsyncTask<String, Void, Void> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(pDialog != null && pDialog.isShowing() == false)
            {   
            pDialog = new ProgressDialog(DatePage.this);
            pDialog.setMessage("Saving...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            }
        }
        /**
         * Creating product
         * */
        protected Void doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            String[] list = dataPorgram.getExParameter("" + programPosition);
            Log.d("programPosition", ""+programPosition);
            params.add(new BasicNameValuePair("exercise", list[0]));
            params.add(new BasicNameValuePair("weight", list[1]));
            params.add(new BasicNameValuePair("reps", list[2]));
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_food,
                    "POST", params);
            if (json == null) {
                Log.d("is null", "null");
                return null;
            }
            // check log cat fro response
            Log.d("Create Response", json.toString());
            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully created product
                    Log.d("ok", json.toString());;
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(Void file_url) {
            if(pDialog != null)
                pDialog.dismiss();
        }
    }
这是我在其他方法中使用的循环
for (int j = 0; j < arrayLength2; j++) {
                    String weight = "";
                    if (helping > 0) 
                    {
                        check1 = true;
                        check2 = true;
                    }
                    if (allWeights[j][2] == 0) {
                        double temp;
                        if (check1)
                            temp = allWeights[j][0] + (allWeights[j][1]);
                        else
                            temp = allWeights[j][0];
                        allWeights[j][0] = temp;
                        DecimalFormat df = new DecimalFormat("#.##");
                        weight = df.format(temp);
                        if (temp == 0)
                            weight = "";
                        else
                            weight = df.format(temp);
                    }
                    else if (allWeights[j][1] == 0) {
                        double temp;
                        if (check2)
                            temp = allWeights[j][0] + (allWeights[j][2] / 100)
                                    * allWeights[j][0];
                        else
                            temp = allWeights[j][0];
                        allWeights[j][0] = temp;
                        DecimalFormat df = new DecimalFormat("#.##");
                        if (temp == 0)
                            weight = "";
                        else
                            weight = df.format(temp);
                        Log.d("weight is ", ""+temp);
                    }
                    programPosition = dataPorgram.setProgramPlan(today,selectedDay, weight, j);
                    Log.d("programPosition", ""+programPosition);
                    new CreateNewWorkout().execute();
我的日志猫:
07-12 15:53:40.131: D/programPosition(9020): 181
07-12 15:53:40.151: D/programPosition(9020): 182
07-12 15:53:40.161: D/programPosition(9020): 183
07-12 15:53:40.161: D/programPosition(9020): 184
07-12 15:53:40.171: D/programPosition(9020): 185
07-12 15:53:40.181: D/programPosition(9020): 186
07-12 15:53:40.181: D/programPosition(9020): 187
07-12 15:53:40.191: D/programPosition(9020): 188
07-12 15:53:40.201: D/weight for server(9020): 70.36
07-12 15:53:40.201: D/programPosition(9020): 188
07-12 15:53:40.841: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:40.841: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:40.851: D/weight for server(9020): 70.36
07-12 15:53:40.851: D/programPosition(9020): 188
07-12 15:53:41.332: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:41.332: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:41.332: D/weight for server(9020): 70.36
07-12 15:53:41.342: D/programPosition(9020): 188
07-12 15:53:41.732: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:41.732: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:41.752: D/weight for server(9020): 70.36
07-12 15:53:41.752: D/programPosition(9020): 188
07-12 15:53:42.253: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:42.253: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:42.263: D/weight for server(9020): 70.36
07-12 15:53:42.263: D/programPosition(9020): 188
07-12 15:53:42.763: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:42.773: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:42.773: D/weight for server(9020): 70.36
07-12 15:53:42.773: D/programPosition(9020): 188
07-12 15:53:43.284: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:43.284: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:43.294: D/weight for server(9020): 70.36
07-12 15:53:43.294: D/programPosition(9020): 188
07-12 15:53:43.684: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:43.684: D/ok(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:43.694: D/weight for server(9020): 70.36
07-12 15:53:43.694: D/programPosition(9020): 188
07-12 15:53:44.205: D/Create Response(9020): {"message":"Product successfully created.","success":1}
07-12 15:53:44.205: D/ok(9020): {"message":"Product successfully created.","success":1}