[英]Android progress dialog not showing in AsyncTask. Blocked UI?
I have an AsyncTask that I'm using to query the database and load a ListView using a custom cursor adapter. 我有一个AsyncTask,用于查询数据库并使用自定义光标适配器加载ListView。 The task itself works, but I cannot get the progress dialog to show up. 该任务本身可以运行,但是无法显示进度对话框。
The async task: 异步任务:
private class LoadListTaskByCursor extends AsyncTask<Void, Void, Cursor> {
private ProgressDialog progressDialog;
private String dictionary;
private Activity activity;
public LoadListTaskByCursor (Activity activity, String dictionary) {
progressDialog = new ProgressDialog(activity);
this.dictionary = dictionary;
this.activity = activity;
}
@Override
protected void onPreExecute() {
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected void onPostExecute(Cursor result) {
ListView lv = (ListView) activity.findViewById(R.id.viewDictionaryList);
lv.setFastScrollEnabled(true);
lv.setAdapter(new CustomTermsCursorAdapter(activity,
R.layout.custom_term_item,
result,
new String[]{getString(R.string.KEY_ID), getString(R.string.KEY_TERM)},
new int[]{R.id.term_item}));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
// Get the term id from this row in the database.
int termId = cursor.getInt(cursor.getColumnIndexOrThrow(getString(R.string.KEY_ID)));
Bundle bundle = new Bundle();
bundle.putInt("id", termId);
Fragment fragment = new ViewTermFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container, fragment)
.addToBackStack(null)
.commit();
}
});
if(progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
@Override
protected Cursor doInBackground(Void... params) {
Cursor cursor = null;
try {
// do the background process
cursor = db.getAllTermListItemsByDictionaryCursor(getString(R.string.TABLE_TERMS), dictionary);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
return cursor;
}
}
And I'm call the task from the fragment like this: 我从这样的片段中调用任务:
LoadListTaskByCursor loadListTaskByCursor = new LoadListTaskByCursor(getActivity(), dictionary);
loadListTaskByCursor.execute();
I've used a similar approach before with a custom array adapter instead of a cursor, and that worked fine. 我之前使用过类似的方法使用自定义数组适配器而不是游标,并且效果很好。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错? Thanks. 谢谢。
It turns out that my doInBackGround was actually running for a really short amount of time (since it is only returning a cursor) while my onPostExecute was taking really long (due to the setAdapter). 事实证明,我的doInBackGround实际上运行了很短的时间(因为它只返回了一个游标),而我的onPostExecute却花费了很长的时间(由于setAdapter的原因)。 The progress dialog was showing up and disappearing without me even noticing. 进度对话框显示并消失,甚至没有引起我的注意。
Thanks to everyone who commented, I appreciate it. 感谢所有发表评论的人,我对此表示赞赏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.