我正在尝试在从Web填充信息时显示进度对话框,该信息填充了创建Listview的sqlite数据库。我创建了一个异步类,在下载信息时显示进度对话框,但之后列表视图为空。这可能是因为列表视图已经在异步之前加载了。那么如何重新加载listview或以正确的方式执行此操作?这是我到目前为止所做的事情(由于显示的时间过长而忽略了后台处理):
private class DownloadInfo extends AsyncTask <Void, Void, Void>{
//////////////////Open Dialog before execution ///////////
private ProgressDialog dialog;
protected void onPreExecute(){
dialog = new ProgressDialog(CandidatesList.this);
dialog.setMessage("Loading Candidates from Webserver");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
///////////////Background Processing//////////////////
@Override
protected Void doInBackground(Void... dbArray) {
////Load information from webserver and put it in sqlite///
return null;
}
///////After the processing is done //////////////
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
on on create如下:
DownloadInfo task = new DownloadInfo();
task.execute();
Cursor c = db.getAllCandidates();
candidates = new String[c.getCount()];
if (c.moveToFirst()) {
int i = 0;
do {
candidates[i] = c.getString(1);// insert Name into candidate
// Array
i++;
} while (c.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
candidates));
请帮忙, 谢谢
答案 0 :(得分:1)
如果要将数据库中的游标信息自动加载到ListView中,请使用SimpleCursorAdapter作为ListView。 (您可能需要在异步完成后调用notifyDataSetChanged()。)