【问题标题】:Progress dialog not appearing in database operation inside AsynTaskAsyncTask 内的数据库操作中未出现进度对话框
【发布时间】:2017-03-21 17:56:12
【问题描述】:

我正在 AysncTask 内的应用程序的本地数据库中插入一些数据,但是在执行课程时,进度对话框没有显示在屏幕上,而我可以看到运行日志。我看到很多相关的答案,但问题没有解决。我阅读了 .get() 方法阻止了 ui,但我已经没有使用这种方法。我不知道为什么它没有显示在屏幕上

从主 Activity 调用异步类

 AsyncGetDataFromServer task = new AsyncGetDataFromServer(this);
 task.execute();

AsyncTask 类的代码

public class AsyncGetDataFromServer extends AsyncTask<Void, Void, Boolean> {

ProgressDialog pd;
Context cxt;
DatabaseHandler dbHelper;
private static ArrayList<DataModel> categoryArrayList;

public AsyncGetDataFromServer(Context context) {
    // TODO Auto-generated constructor stub
    cxt= context;
    pd = new ProgressDialog(cxt);
    pd.setTitle("Please wait");
    pd.setMessage("Loading...");
    pd.setCancelable(false);
    dbHelper = new DatabaseHandler(cxt);
}

@Override
protected Boolean doInBackground(Void... params)
{
    try {
        Log.d("do in background","true");

                for (int i = 0; i < response.body().getVideoEntity().size(); i++) {
                    //inserting in categories
                    VideoEntity videoEntity;
                    videoEntity = response.body().getVideoEntity().get(i);
                    dbHelper.insertChannels(videoEntity);
                }        
            }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e("exception error", e.getMessage());
    }

    return true;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Log.d("on pre execute","true");
    pd.show();
}

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    Log.d("on post execute","true");
    pd.dismiss();
}

}

【问题讨论】:

  • 将所有写在构造函数中的代码放到onPreExecuate方法中,然后chk并移除那个构造函数
  • 我已经测试过了,但是没有用。
  • 用上面的代码试试这个新的 AsyncGetDataFromServer().execute();
  • @Sumant 一开始闪过就消失了。
  • G8.. 看看你的循环执行了多少次?如果你想要更多的时间,暂时只是添加计时器到 chk,进度对话框会显示几秒钟

标签: android sqlite android-asynctask


【解决方案1】:

将 Activity 而不是上下文传递给构造函数。例如-

    AsyncGetDataFromServer task = new AsyncGetDataFromServer(MyActivity.this);
 task.execute();

【讨论】:

    【解决方案2】:

    你应该实现方法onProgressUpdate并使用方法publishProgress

    https://developer.android.com/reference/android/os/AsyncTask.html

    【讨论】:

      【解决方案3】:

      在 onPreExecute() 方法中显示对话框并在 onPostExecute() 方法中关闭:

      private class AsyncGetDataFromServer extends AsyncTask<Void, Void, Boolean> {
        private final ProgressDialog dialog = new ProgressDialog(YourClass.this);
      
        protected void onPreExecute() {
           this.dialog.setMessage("Loading...");
           this.dialog.show();
        }
      
        protected void doInBackground(final Void unused) {
           //don't interact with the ui!
        }
      
        protected void onPostExecute(final Boolean result) {
           if (this.dialog.isShowing()) {
              this.dialog.dismiss();
           }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多