views:

245

answers:

2

I have some data I load into the database the first time a user enters my Activity, and want to show a ProgressDialog while this data is loaded for the first time. My Activity is an ExpandableListActivity and I don't create the SimpleExpandableListAdapter or call setListAdapter passing my adapter until I'm sure the data is actually there. My onCreate looks like this:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCategoryDbHelper = new CategoryDBHelper(this);

        // Build default categories... if not there yet
        CategoryDbBuilder builder = new CategoryDbBuilder(this);
        if (!builder.hasCategoriesInTable()) {
            showDialog(PROGRESS_DIALOG_ID);
            builder.fillDbWithDefaultCategories();
            removeDialog(PROGRESS_DIALOG_ID);
        }

        populate();
    }

I've overwritten onCreateDialog as such:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case PROGRESS_DIALOG_ID: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("Loading categories for the first time. Please wait...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(false);
                return dialog;
            }       
        }
        return null;
    }

The populate() method reads the database and sets up my list adapter, then calls setListAdapter.

This seems like it should be simple, but it's turning out to be a huge pain. Any help would be appreciated. :-)

+2  A: 

Just use this simple line:

mProgressDialog = ProgressDialog.show(context, "", "msg to show", true);

You can dismiss it with:

mProgressDialog.dismiss();
Macarse
I've tried that, it doesn't work. Nothing shows.
Scienceprodigy
It should. Check if your imports are ok. This should work.
Macarse
Nope. No dice. Everything is fine. I got it to work by spawning another thread and doing builder.fillDbWithDefaultCategories() in the run() method.
Scienceprodigy
new Thread? why? This should run on the UI thread.
Macarse
The UI thread was being blocked by my method call.
Scienceprodigy
+2  A: 

Use AsynTask put your database loading processing in background function and in post execution display result. and there is another function to processing something until background process running here is example of asynTask

http://stackoverflow.com/questions/2105364/android-i-want-to-show-file-upload-progress-to-the-user/3565458#3565458

ud_an
Yeah, that's one way of doing it, however AsyncTask had a problem with me trying to make the thread sleep for a few seconds so the progress dialog would stay up for a bit and not just flash by. I ended up doing my processing in a new thread, and killed the progress dialog in a Handler.
Scienceprodigy