开发者

Could ProgressDialog's style, title and message be changed on the fly?

开发者 https://www.devze.com 2023-02-25 00:00 出处:网络
Is it possible to change PD\'s style, message and title on the fly (and not from UI thread)? I want to do the following. Fist time PD shows in STYLE_SPINNER as it looks like endless progress and show

Is it possible to change PD's style, message and title on the fly (and not from UI thread)?

I want to do the following. Fist time PD shows in STYLE_SPINNER as it looks like endless progress and shows the message informing user that app is looking 4 something (some update 4 instance). And when it found that something it has to do something with it (install it). At this point i wanna show the HORIZONTAL styled PD as it really shows the progress state instead of spinner styled one.

Anyway I got NullPointerException while trying to PD.setMax(). PD isn't null so I can't get what's going on.

This is an inner class in my Activity class:

    private class RestoreDBTask extends AsyncTask <Void, Void, String>
    {

        private ProgressDialog dialog;

        private Handler handler;

        @Override
        protected void onPreExecute()
        {

            this.dialog = new ProgressDialog(SplashActivity.this);
            this.dialog.setTitle(getString(R.string.progress_wait));
            this.dialog.requestWindowFeature(Window.FEATURE_PROGRESS);
//            this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            this.dialog.setProgress(0);
            this.dialog.show();

            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    RestoreDBTask.this.dialog.hide();

                    switch (msg.what) {
                    case 0:{
                        RestoreDBTask.this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        break; }
                    case 1: {
                        RestoreDBTask.this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        break; }
                    case -1: {
                        RestoreDBTask.this.dialog.setMessage(getResources().getString(R.string.progress_db_installing));
                        break; }
                    case -2: {
                        RestoreDBTask.this.dialog.incrementProgressBy(1);
                        break; }
                    default: {
                        Log.i(APP_TAG,""+RestoreDBTask.this.dialog.getMax());
                        RestoreDBTask.this.dialog.setMax(msg.what); }
                    }

                    RestoreDBTask.this.dialog.show();
                }
            };
        }

        @Override
        protected String doInBackground(Void... params)
        {
            mDBHelper.initDB(dialog,handler);
            return "";
        }

        @Override
        protected void onPostExecute(String result)
        {
            dialog.dismiss();
            startNextActivity();
        }
    }

the mDBHelper.initDB() method uses handler.sendEmptyMessage() calls with some int values assuming if msg.what>1 it is Max value for PD.

Also I can't figure out why PD doesn't show the progress state if it's style is STYLE_SPINNER? If i change it to STYLE_HORIZONTAL开发者_如何学Go it works ok...


I've encountered that PD's style couldn't be changed after show() method was executed (but only b4) because is causes a NullPointerException if app tries to call setMax()/setProgress()/increment etc. methods after that. Moreover PD's style doesn't change visually at all even in case of using hide()->change style->show() schema.

My solution is to recreate PD in case of changing it's style (from spinner to bar or clockwise). But the problem is - I cant get Title and Message of existing PD to copy it to the new PD due to corresponding methods (getTitle/getMessage) doesn't exists and thats stupid cause setter methods do exists so it breaks Java beans rules. Why didn't they provide getters?

Unlike PD's style it's title and message could be changed on the fly.


Is it possible to change PD's style, message and title on the fly (and not from UI thread)?

It's not possible to update UI stuff in another Thread. What you should do is to override onProgressUpdate() and there you can update the UI while the code in doInBackground() still processing.

Sample code:

@Override
protected String doInBackground(Void... params) {

    // Use publishProgress() to update the UI thread from
    // a working background process.

    // If you have the initDB in another class you should probably
    // pass your RestoreDBTask instance to your initDB and call the instance's
    // makeProgress() method to successfully use publishProgress().
    mDBHelper.initDB(dialog, handler, this);
    return "";
}

@Override
protected void onProgressUpdate(Integer... params) {
    // If you gradually should update the ProgressDialog you probably need
    // an Integer value as argument in this method.

    // Update the ProgressDialog here.
}

public void makeProgress(Integer... params) {
    publishProgress(params);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消