Android 进度对话框未出现在AsynTask内的数据库操作中
我在AYSNTASK中的应用程序本地数据库中插入了一些数据,但在执行类时,进度对话框不会显示在屏幕上,而我可以看到运行日志。我看到了许多相关的答案,但问题没有得到解决。我读取了.get()方法阻止ui,但我已经没有使用此方法。我不知道为什么它没有显示在屏幕上 从主活动调用异步类Android 进度对话框未出现在AsynTask内的数据库操作中,android,sqlite,android-asynctask,Android,Sqlite,Android Asynctask,我在AYSNTASK中的应用程序本地数据库中插入了一些数据,但在执行类时,进度对话框不会显示在屏幕上,而我可以看到运行日志。我看到了许多相关的答案,但问题没有得到解决。我读取了.get()方法阻止ui,但我已经没有使用此方法。我不知道为什么它没有显示在屏幕上 从主活动调用异步类 AsyncGetDataFromServer task = new AsyncGetDataFromServer(this); task.execute(); 异步任务类的代码 public class Async
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();
}
公共类AsyncGetDataFromServer扩展了AsyncTask{
进展性帕金森病;
上下文cxt;
数据库处理程序dbHelper;
私有静态数组列表类别数组列表;
公共AsyncGetDataFromServer(上下文){
//TODO自动生成的构造函数存根
cxt=上下文;
pd=新进度对话框(cxt);
pd.setTitle(“请稍候”);
pd.setMessage(“加载…”);
pd.可设置可取消(假);
dbHelper=新数据库处理程序(cxt);
}
@凌驾
受保护的布尔doInBackground(Void…params)
{
试一试{
Log.d(“背景中的do”、“真”);
对于(int i=0;i
}将活动而不是上下文传递给构造函数。例如-
AsyncGetDataFromServer task = new AsyncGetDataFromServer(MyActivity.this);
task.execute();
将活动而不是上下文传递给构造函数-
AsyncGetDataFromServer task = new AsyncGetDataFromServer(MyActivity.this);
task.execute();
您应该实现方法
onProgressUpdate
并使用方法publishProgress
:
请参见您应该在ProgressUpdate上实现方法
并使用方法publishProgress
:
请参见onPreExecute()方法中的显示对话框和onPostExecute()方法中的解除:
私有类AsyncGetDataFromServer扩展了AsyncTask{
private final ProgressDialog=新建ProgressDialog(YourClass.this);
受保护的void onPreExecute(){
this.dialog.setMessage(“加载…”);
this.dialog.show();
}
受保护的空位背景(最终空位未使用){
//不要与ui交互!
}
受保护的void onPostExecute(最终布尔结果){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
}
}
在onPreExecute()方法中显示对话框,并在onPostExecute()方法中关闭:
私有类AsyncGetDataFromServer扩展了AsyncTask{
private final ProgressDialog=新建ProgressDialog(YourClass.this);
受保护的void onPreExecute(){
this.dialog.setMessage(“加载…”);
this.dialog.show();
}
受保护的空位背景(最终空位未使用){
//不要与ui交互!
}
受保护的void onPostExecute(最终布尔结果){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
}
}
将构造函数中编写的所有代码放到onPreExecuate方法和chk中,并删除该构造函数,或者测试它,但不起作用。使用上述代码并尝试此新的AsyncGetDataFromServer().execute()@苏门特:它一开始就闪过,然后消失了。。看看你的循环执行了多少次?如果您需要更多时间,暂时只需将计时器添加到chk,将显示进度对话框几秒钟。将构造函数中写入的所有代码放到onPreExecuate方法和chk中,并删除该构造函数。或者测试它,但它已经不起作用。使用上述代码,然后尝试此新的AsyncGetDataFromServer().execute()@苏门特:它一开始就闪过,然后消失了。。看看你的循环执行了多少次?若你们想要更多的时间,暂时只需将定时器添加到chk,进度对话框会显示几秒钟