android - 在 SQLite 中下载和插入 JSON 数据时如何更新 android 中的 Progressbar?
问题描述
将 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-loop 可能是这种情况,但我找不到如何将该值传递给 publishprogress(); 的方法。方法。
日志视图:
Log.i("view_", "size: " + size + " " + "counter: " + counter + " " + "percent: " + percent + "%");
请帮我解决这个问题。几周以来,我一直被这个问题困扰。先感谢您!!
解决方案
首先,根据android文档永远不要手动调用onPreExecute()
,,,,。(您正在手动调用)onPostExecute(Result)
doInBackground(Params...)
onProgressUpdate(Progress...)
onProgressUpdate
doInBackground
其次,移动publishProgress(value);
到 try 块内的 for 循环。
推荐阅读
- docker - 安装在 Docker/Alpine Linux 上的 Db2 ODBC 驱动程序
- authentication - 受 IsAuthenticated() 保护的 API 端点上带有 SWAGGER 2.2.0 的 Django3.0.5 和 DRF 问题未在招摇中显示
- python - Pandas read_csv 不忽略引号字符串中的逗号
- c++ - 在Makefile中链接Boost?
- sql-server - SQL Server:全文索引初始填充性能
- reactjs - 为什么我不能在 useEffect 函数中使用 props 属性?
- c# - 另一个文件中包含的 T4 缩进代码
- node.js - 猫鼬聚合排序无法正常工作
- javascript - Javascript JSON.stringify 更改包含反斜杠的字符串
- python - Pandas pd 没有填充 .append