[英]Asynctask onPostExecutive method assigning a value to instance variable but it is not showing that in mainActivity class.
[英]Class variable in a AsyncTask showing wrong value
我试图从循环调用AsyncTask并为我的AsyncTask更新varbile类。
我的问题是, programPosition
不会按实际值更新,只给我
循环中的最后一个值。
看我的logcat programPosition
位置在我的循环中正在更新,但是我的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}
您是否在多个线程上使用此值? 如果该值为int,则将其替换为AtomicInteger并将其打开是线程安全的。 对于对象,您可以使用atomicreference。
上述解决方案将使您能够更新分布在多个线程中的值并安全地读取它们。 如果要访问(更新但还要读取)在另一个线程中更改的数据,则需要对该数据进行一些同步。 doInBackground方法在其他线程中运行,除非已正确同步,否则不应访问其他线程的数据。
您能解释一下您要做什么吗? 因为我完全不清楚您的程序的目的是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.