【发布时间】: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