我有一些数据在用户第一次进入我的 Activity
时加载到数据库中,并且想在第一次加载这些数据时显示一个 ProgressDialog
.我的 Activity 是一个 ExpandableListActivity
,我不会创建 SimpleExpandableListAdapter
或调用 setListAdapter
传递我的适配器,直到我确定数据确实存在.我的 onCreate
看起来像这样:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCategoryDbHelper = new CategoryDBHelper(this);
// Build default categories... if not there yet
CategoryDbBuilder builder = new CategoryDbBuilder(this);
if (!builder.hasCategoriesInTable()) {
showDialog(PROGRESS_DIALOG_ID);
builder.fillDbWithDefaultCategories();
removeDialog(PROGRESS_DIALOG_ID);
}
populate();
}
我已经这样覆盖了 onCreateDialog
:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG_ID: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading categories for the first time. Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
return dialog;
}
}
return null;
}
populate()
方法读取数据库并设置我的列表适配器,然后调用 setListAdapter
。
这看起来应该很简单,但事实证明这是一个巨大的痛苦。任何帮助,将不胜感激。 :-)
最佳答案
使用 AsynTask 将您的数据库加载处理放在后台函数和执行后显示结果中。还有另一个函数可以处理一些事情,直到这里运行的后台进程是 asynTask 的例子
关于Android:进度对话框不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3597325/