PhillyNJ
PhillyNJ

Reputation: 3903

ListActivity and Dialogs

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

Answers (2)

Noureddine AMRI
Noureddine AMRI

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

Related Questions