开发者

How do I show and then remove an android progress dialog

开发者 https://www.devze.com 2023-01-29 05:55 出处:网络
I can get a progress bar to appear with the following code pd = ProgressDialog.show(myActivity.this, \"\", \"Loading. Please wait...\", true);

I can get a progress bar to appear with the following code

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);

straight forward, but once I have the code execute I want it to go away, but when I run the dismiss method after I never see the dialog box show at all.

Heres the code in context which is wrapped in oncreate

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);
runCode();  
setListAdapter(new CustomAdapter开发者_如何转开发(myActivity.this)); 
pd.dismiss();

I thought you can show/dismiss progress dialog's anywhere in the activity but I must be wrong.


here is the code that I got to work

private class UpdateFeedTask extends AsyncTask<MyActivity, Void, Void> {

    private ProgressDialog mDialog;

    protected void onPreExecute() {
        Log.d(TAG, " pre execute async");
        mDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);

    }

    protected void onProgressUpdate(Void... progress) {
        Log.d(TAG, " progress async");     
    }

    @Override
    protected Void doInBackground(MyActivity... params) {
        // TODO Auto-generated method stub
        return null;
    }

    protected void onPostExecute(Void result) {
        Log.d(TAG, " post execute async");
        mDialog.dismiss();
    }

}


refer the sample code sinnpet of mine,hope this may help you

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ProgressBar;

public class PlayActivity extends Activity {
    /** Called when the activity is first created. */

  private ProgressBar mProgress;
     private int mProgressStatus = 0;
     private int count=0;
     private Handler mHandler=new Handler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
        new Thread(myThread).start();

    }
    private Runnable myThread = new Runnable(){

     @Override
     public void run() {
     // TODO Auto-generated method stub
     while (mProgressStatus<=100){
     try{
     myHandle.sendMessage(myHandle.obtainMessage());
     Thread.sleep(1000);
     }
     catch(Throwable t){
     }
     }
     }

     Handler myHandle = new Handler(){

     @Override
     public void handleMessage(Message msg) {
     // TODO Auto-generated method stub
      mProgressStatus=mProgressStatus+10;
      count=mProgressStatus;
      Log.d("mProgressStatus",Integer.toString(count));
      mProgress.setProgress(mProgressStatus);
      if(count > 80)
            {
             Log.d("mProgressStatus",Integer.toString(mProgressStatus));
             counter.start();
            }
     }
     };
     };



}


To remove a progressBar

ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);

progressBar.setVisibility(View.INVISIBLE) ;

hide the progressbar in xml and show it like

 ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);

    progressBar.setVisibility(View.VISIBLE) ;
0

精彩评论

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

关注公众号