Asynctask和进度条计时

时间:2013-04-17 11:57:23

标签: android

在对话框中按ok后,我应该从其他地方获取数据。所以我使用了Asynctask类。实施如下。但是在按下第一个确定之后我就不会得到任何进度条。按下第二个ok后我才会得到它(基本上,当按钮onClick方法中的所有行都被执行时... 按下第一个确定后我该怎么办才能获得进度条?

confirmPath= (Button) findViewById(R.id.confirmPath);
            confirmPath.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {

                        construction
                        AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
                        builder.setMessage("Press 'Ok' ")
                               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {

                                       LoadData task = new LoadData();
                                       task.execute();


                                                                                AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
                                    alertDialog.setTitle("Attention!");
                                    alertDialog.setMessage(" Pay attention");
                                     alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int which) {
                                           }
                                        });

                                        alertDialog.show();

                                   }
                               })
                               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {
                                       // do nothing
                                   }
                               });
                       builder.create();
                       builder.show();

这是AsynkTask类:

public class LoadData extends AsyncTask<Void, Void, Void> {
        ProgressDialog progressDialog;
        //declare other objects as per your need
        @Override
        protected void onPreExecute()
        {
            progressDialog= ProgressDialog.show(Destination.this, "Progress Dialog Title Text","Please wait", true);

            //do initialization of required objects objects here                
        };      
        @Override
        protected Void doInBackground(Void... params)
        {   

               ReadFromFile readFromFile= new ReadFromFile();
               readFromFile.ReadAllData("Data");
               //some other tasks to do

            return null;
        }       
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            progressDialog.dismiss();
        };
     }

3 个答案:

答案 0 :(得分:0)

将此代码放在AlertDialogBu​​ilder之外:

LoadData task = new LoadData();
task.execute();

新代码应如下所示:

 @Override
            public void onClick(View arg0) {


                                       LoadData task = new LoadData();
                                       task.execute();


                        construction
                        AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
                        builder.setMessage("Press 'Ok' ")
                               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {


                                                                                AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
                                    alertDialog.setTitle("Attention!");
                                    alertDialog.setMessage(" Pay attention");
                                     alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int which) {
                                           }
                                        });

                                        alertDialog.show();

                                   }
                               })
                               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {
                                       // do nothing
                                   }
                               });
                       builder.create();
                       builder.show();

答案 1 :(得分:0)

使用此代码

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

    private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

    protected String doInBackground(String... pass) {

        try {


            //userid is getting from webservice             

        return userid;

        } catch (Exception e) {

            System.out.println(e);
        }




    }

    protected void onPostExecute(String result) {

        Dialog.dismiss();

        if (result == null) {
            tv.setText("Enter Your Password Correctly");
            pwet.setFocusable(true);

        } else {



        }

    }

    protected void onPreExecute() {

        Dialog.setMessage("Loading...");
        Dialog.show();

    }

}

答案 2 :(得分:0)

我应该将第二个alertdialog也放在asynctask onPostExecute方法中。然后我显然有进度条