0

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

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();
        };
     }
4

3 回答 3

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();

    }

}
于 2013-04-17T12:15:26.657 回答
0

I should have put the second alertdialog also in asynctask onPostExecute method. then I have the progressbar before obviously

于 2013-04-17T12:41:32.737 回答
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();
于 2013-04-17T12:06:09.513 回答