0
我試圖顯示一個進度對話框,同時從web上填充信息,填充從中創建ListView的sqlite數據庫。我創建了一個異步類,在下載信息時顯示進度對話框,但在此之後,列表視圖爲空。這可能是因爲在異步之前已經加載了listview。那麼,如何重新加載列表視圖或以正確的方式進行操作呢?以下是我迄今爲止(留出了後臺處理,因爲它太長時間displag):Android:從數據庫異步填充後從數據庫中加載列表視圖
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));
請幫幫忙, 謝謝
在postExecute中調用notifyDataSetChanged()?而在什麼? notifyDataSetChanged()是SimpleCursorAdapter的繼承方法。 – Diaa 2012-04-20 19:37:47
一種解決方案是將您的AsyncTask傳遞給您的適配器,並且在postExecute()中調用notifyDataSetChanged()。 – Sam 2012-04-20 19:50:31