Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

android - show progressbar on button click when going from 1 intent to other and data is coming from server

I have a button in one page and when I click on that button I am able to go another activity through Intent(), but onbuttonclick() in which activity I am going in that activity data in spinner coming from server means on button click I load that data on spinner from server.so it takes times for moving my button click activity to other activity so I want to show progress bar when my button is clicked and untill data is not coming from server...how to achieve this..and I want to show progress bar on buttonclick page means on my first activity when I click the button.

My code of of on button click is given below.

cuurentloc.setOnClickListener(new View.OnClickListener()
        {
      public void onClick(View v) {
      Intent i = new Intent(MainMenu.this, currentlocmap.class);
    startActivity(i);

     }
      });

Actually I know asynchronous task but using this I will be able to show progress bar on 2nd activity, I want to show it on my first activity until data is not loaded in second activity, so I want progree bar above the button on first activity, and when data is loaded on second activity it moves to second.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use AsyncTask as the way I am guiding here. Create Async Task in first activity. On button click event call that AsyncTask. In background do loading data from server. and onPostExecute start second activity

cuurentloc.setOnClickListener(new View.OnClickListener()
        {
      public void onClick(View v) {
      new ProgressTask(MyClassName.class).execute(null);

     }
      });

Async Task

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        List<Message> titles;
        private ListActivity activity;
        //private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }



        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

            @Override
        protected void onPostExecute(final Boolean success) {

                if (dialog.isShowing()) {
                dialog.dismiss();
            }

            Intent i = new Intent(MainMenu.this, currentlocmap.class);
    startActivity(i);

        }

        protected Boolean doInBackground(final String... args) {
            try{    
                //load data from server
             } catch (Exception e){
                Log.e("tag", "error", e);
                return false;
             }
          }


    }

}

Thanks Deepak


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...