Reputation: 3903
I have a ListActivity
that requires a lot of data to be processed via a Thread
. I want to show a Dialog
while the work is being done in the background
. However, the Dialog
doesn't show until the view is done loading. I use this approach on a regular activity
and it works(Dialog
pops up, worker starts, worker finishes, Dialog
dismisses). When the work is done, a message is sent the handler
to load the view
and dismiss the Dialog
.
Here is my code:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
initViewProperties();
dialogCode = 0;
showDialog(dialogCode);
}
@Override
protected Dialog onCreateDialog(int id) {
int resID = getResources().getIdentifier(getCfrFile(), "raw", getPackageName());
progDialog = new ProgressDialog(this);
switch (dialogCode) {
case 0:
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage(getCurrentTitle());
break;
case 1:
progDialog.setMessage("Loading CFR");
break;
default:
break;
}
try {
// do the work here
xmlListRowFactoryHelper = new AcmListViewFactoryHelper(handler, resID, this, xpathBuilder(false), "title") ;
xmlListRowFactoryHelper.run();
} catch (Exception e) {
e.printStackTrace();
}
return progDialog;
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
progDialog.setProgress(total);
if (total <= 0){
xmlListRowFactoryHelper.setState(AcmTableRowFactoryHelper.DONE);
// load the view here
loadListView();
}
}
}
Upvotes: 0
Views: 111
Reputation: 42016
use Asyntask concept which remove complex threading concept http://developer.android.com/reference/android/os/AsyncTask.html
example http://www.vogella.de/articles/AndroidPerformance/article.html
Upvotes: 1
Reputation: 2942
Use AsyncTask for that, it's nice and easy. See the example and explanation here: http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 1