2016-11-08 68 views
1

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

从主要活动异步调用类的AsyncTask类

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

代码

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(); 
} 

}

+0

把所有的代码中写道:构造函数onPreExecuate方法和chk并删除构造函数 – Sumant

+0

我已经测试过,但没有奏效。 – Andrain

+0

使用上面的代码并尝试这个新的AsyncGetDataFromServer()。execute(); – Sumant

回答

0

通活动,而不是上下文例如 -

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

显示对话框()方法&驳回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(); 
    } 
    } 
}