下载或插入JSON数据到数据库的后台或线程操作工作正常,但由于我也想更新ProgressBar,所以它以零百分比开始,并且在整个操作完成而没有任何更新时关闭。
同步片段:
String value; //defined globally
private class JsonParse extends AsyncTask<String, String, String> {
Context mcontext;
private ProgressDialog progressDialog;
public JsonParse(Context context) {
this.mcontext = context;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mcontext);
progressDialog.setTitle("Downloading"); // Setting Title
progressDialog.setMessage("Please wait"); // Setting Message
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setCancelable(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, mcontext.getString(R.string.cancel_btn), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String choice = params[0];
final String url = BASE_URL + "" + VERSION + "" + METHOD + "";
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("response", url + "response:" + response);
try {
JSONObject object = new JSONObject(response);
JSONArray jsonArray = object.getJSONArray(METHOD + "");
long counter = 0;
int size = jsonArray.length();
for (int i = 0; i <= size; i++) {
final JSONObject jsonObject = jsonArray.getJSONObject(i);
counter++;
int percent = (int) ((counter * 100) / size);
value = percent + "";
onProgressUpdate(value);
Log.i("view_", "size: " + size + " " + "counter: " + counter + " " + "percent: " + percent + "%");
switch (choice) {
case "deaths":
String id_d = jsonObject.getString("id");
String family_id = jsonObject.getString("family_id");
String death_one_year = jsonObject.getString("death_one_year");
String no_of_deaths = jsonObject.getString("no_of_death");
String cause = jsonObject.getString("cause");
String cause_other = jsonObject.getString("cause_other");
String ward_id_d = jsonObject.getString("ward_id");
String has_error_d = jsonObject.getString("has_error");
String updated_by_d = jsonObject.getString("updated_by");
String updated_at_d = jsonObject.getString("updated_at");
String created_at = jsonObject.getString("created_at");
String uuid_d = jsonObject.getString("uuid");
dbHelper.SyncDeath(Integer.parseInt(id_d), family_id, death_one_year, no_of_deaths, cause, cause_other,
ward_id_d, has_error_d, updated_by_d, updated_at_d, created_at, uuid_d);
death_txt.setTextColor(getResources().getColor(R.color.colorPrimary));
death_txt.setText(R.string.synced);
break;
}
}
return;
} catch (JSONException e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(getContext(), "Server Error, Please Try Again!!", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
publishProgress(value);
Log.i("view_percent_value", value); // Getting null value
/*for (int i = 0; i <= 100; i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(String.valueOf(i));
}*/
return null;
}
@Override
protected void onProgressUpdate(String... values) {
progressDialog.setProgress(Integer.parseInt(values[0]));
value = values[0]; // Tried to update this "value" i.e. set globally but can't update
Log.i("view_values", values[0] + "");
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
}
}
上面提到的代码:
/*for (int i = 0; i <= 100; i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(String.valueOf(i));
}*/
可以帮助我更新ProgressBar,但不能实时更新。就我而言,该参数在publishprogress()中传递;即全局定义的“值”不会从循环中获取更新的值。我猜想,在try-catch中使用for循环可能是这种情况,但是我找不到如何将该值传递给publishprogress()的方法;方法。
“日志”视图:
Log.i("view_", "size: " + size + " " + "counter: " + counter + " " + "percent: " + percent + "%");
请帮助我解决此问题。几个星期以来,我一直在解决这个问题。预先谢谢你!
答案 0 :(得分:1)
第一,根据android documents,切勿手动调用onPreExecute()
,onPostExecute(Result)
,doInBackground(Params...)
,onProgressUpdate(Progress...)
。
(您正在手动调用onProgressUpdate
中的doInBackground
)
第二,将publishProgress(value);
移动到try块内的for循环。
答案 1 :(得分:0)
尝试使用onProgressUpdate()
功能设置进度。
点击此链接以供参考: android how to work with asynctasks progressdialog