這與AsyncTask幫助(智能底色線程)和ProgressDialog
時候做的AsyncTask開始,我們提出一個progressdialog與不確定狀態,一旦任務完成後,我們關閉對話框。
示例代碼
什麼適配器確實在這個例子就明白了,你需要使用的AsyncTask以顯示該進程的對話框並不重要,更重要的。
private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(viewContacts.this);
dialog.setMessage(getString(R.string.please_wait_while_loading));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected ContactsListCursorAdapter doInBackground(Void... params) {
cur1 = objItem.getContacts();
startManagingCursor(cur1);
adapter1 = new ContactsListCursorAdapter (viewContacts.this,
R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});
return adapter1;
}
protected void onPostExecute(ContactsListCursorAdapter result) {
list.setAdapter(result);
dialog.dismiss();
}
}
親愛的Pentium10,謝謝你的回答!這可能對我有很大幫助,儘管我的應用程序中耗時的部分是對ListAdapter的getView()方法的調用。但無論如何:是否有必要爲onCreate方法中的ListView提供適配器?他們是怎麼做到的?有一個空虛擬列表或什麼?另外,這個加載動畫的白色屏幕只是一個覆蓋整個屏幕的Progressdialog?提前致謝! – janoliver 2010-11-20 19:27:08
您可以在列表視圖上提供適配器,即使它是空的。然後,您可以稍後添加項目,並且可以調用notifyDatasetChanged事件來刷新ListView。您可以將視圖緩存在某個類對象中供以後使用。 – Pentium10 2010-11-20 20:08:41