0

我试图在从 Web 加载信息时显示进度对话框,该信息填充了创建 Listview 的 sqlite 数据库。我创建了一个异步类,它在下载信息时显示进度对话框,但之后列表视图为空。这可能是因为列表视图在异步之前已经加载。那么如何重新加载列表视图或以正确的方式执行此操作?这是我到目前为止所拥有的(因为它太长而无法显示,所以省略了后台处理):

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 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));

请帮忙,谢谢

4

1 回答 1

1

如果要将游标信息从数据库自动加载到 ListView 中,请为 ListView使用SimpleCursorAdapter 。(您可能需要在异步完成后调用 notifyDataSetChanged()。)

于 2012-04-20T19:27:53.407 回答