Reputation: 149
The background or thread operation of downloading and inserting JSON data into database works fine but as I also want to update my ProgressBar when the data insertion operation keeps updating. My problem is, it starts with Zero percent and dismisses when the whole operation is completed without any update in percentage value.
Sync Fragment:
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) {
}
}
The above-commented code:
/*for (int i = 0; i <= 100; i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(String.valueOf(i));
}*/
helps me update ProgressBar but it is not updating in Realtime. In my case, the parameter passed in publishprogress(); i.e. "value" which is defined globally is not getting updated value from the loop. As I guess, it may be the case using for-loop inside try-catch but I can't find the way how to pass that value to publishprogress(); method.
The Log view:
Log.i("view_", "size: " + size + " " + "counter: " + counter + " " + "percent: " + percent + "%");
is updating the percentage value in real-time. Like this:
Please help me to figure out this problem. I'm stuck with this problem from couple of weeks. Thank you in advance!!
Upvotes: 0
Views: 819
Reputation: 908
First, according to android documents never call onPreExecute()
, onPostExecute(Result)
, doInBackground(Params...)
, onProgressUpdate(Progress...)
manually.
(you are calling onProgressUpdate
in doInBackground
manually)
Second, move publishProgress(value);
to for loop inside try block.
Upvotes: 1
Reputation: 1253
Trying setting value of your progress in onProgressUpdate()
function.
Follow this link for reference: android how to work with asynctasks progressdialog
Upvotes: 0