我想在listview加载时显示ProgressDialog。我已经尝试过如下编码,但它没有用。
final ProgressDialog dialog = ProgressDialog.show(this, "indeterminate", "Please wait while loading", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
// listview will be populated here
PopulateListView();
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
但它根本不起作用。
答案 0 :(得分:4)
这是填充ListView
的代码,其中包含使用自定义适配器和加载时ProgressDialog
的数据库中的任何内容
public class whateverListActivity extends ListActivity
{
private ProgressDialog mProgressDialog = null;
private ArrayList<Whatever> mWhatevers = null;
private WhateverAdapter mAdapter;
private Runnable mViewWhatevers;
private SQLiteDatabase mDatabase;
private WhateverHelper mWhateverHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.whatever_list);
mWhatevers = new ArrayList<Whatever>();
mAdapter = new WhateverAdapter(this, R.layout.whatever_row, mWhatevers);
setListAdapter(this.mAdapter);
SQLiteOpenDataHelper helper = new SQLiteOpenDataHelper(this);
mDatabase = helper.getWritableDatabase();
}
@Override
protected void onResume() {
try{
super.onResume();
mWhateverHelper = new WhateverHelper(this,mDatabase);
startGetWhatevers();
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
}
private void startGetWhatevers(){
mViewWhatevers = new Runnable(){
public void run() {
getWhatevers();
}
};
Thread thread = new Thread(null, mViewWhatevers, "Background");
thread.start();
mProgressDialog = ProgressDialog.show(this,
"Please wait...", "Retrieving data ...", true);
}
private void getWhatevers(){
try{
mWhatevers = new ArrayList<Whatever>();
for (Whatever whatever : mWhateverHelper.getCurrentWhatevers()) {
mWhatevers.add(whatever);
}
Thread.sleep(1500);
Log.i("ARRAY", ""+ mWhatevers.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
private Runnable returnRes = new Runnable() {
public void run() {
mAdapter.clear();
if(mWhatevers != null && mWhatevers.size() > 0){
mAdapter.notifyDataSetChanged();
for(int i=0;i<mWhatevers.size();i++)
mAdapter.add(mWhatevers.get(i));
}
mProgressDialog.dismiss();
mAdapter.notifyDataSetChanged();
}
};
}
希望这会有所帮助。 亚历克斯。
答案 1 :(得分:1)