当从web加载信息时,我试图显示一个进度对话框,该信息填充了一个sqlite数据库,并从中创建了一个Listview。我创建了一个异步类,它在下载信息时显示进度对话框,但之后listview为空。这可能是因为在异步之前已经加载了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();
}
}创建时的代码如下:
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));请帮帮忙,谢谢
发布于 2012-04-21 03:27:53
如果要将游标信息从数据库自动加载到SimpleCursorAdapter中,请使用ListView的ListView。(您可能需要在异步完成后调用notifyDataSetChanged()。)
https://stackoverflow.com/questions/10252245
复制相似问题