i have run into the same a long time ago, but got it solved, sorry that the code is not commented , but any doubts i will be glad to help you.
This is the code for a filling a ListView
with a whatever from a DB using a custom adapter an a ProgressDialog
while loading
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();
}
};
}
Hope this helps.
Alex.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…