开发者

My AsyncTask runs even when its if statement conditions aren't met

I want to display an alert dialog and get the user input on whether or not to run the AsyncTask. However the AsyncTask runs anyway, even when I put it in an if statement. Does anyone know why this is happening? This is my code:

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Boolean b = false;

            AlertDialog.Builder alertbox = new AlertDialog.Builder(
                    ThisScreen.this);
            alertbox.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            b = true;
                        }
                    });
            alertbox.setPositiveButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1开发者_开发问答) {
                            b = false;
                        }
                    });

            alertbox.setTitle("Title");
            alertbox
                    .setMessage("Continue?");
            alertbox.show();

            if(b)
                new doAsyncTask().execute;

    }
});


I don't know if it makes any difference but the logical thing would be to set the no-button using the method setNegativeButton().


You should move the code to run the task inside

alertbox.setPositiveButton("Yes",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            runMyDoAsyncTask(); // <-- here
        }
    });

btw I'm not sure that the code you provided will ever compile :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜