0

Anybody knows how to put AlertDialog in AsyncTask? I have application that using WebService. So I have put Ip address in order to start the application. Currently I have to put default IP address and then I change it using AlertDialog, so like settings->insert ip. Right now I would like to each time when the application start, the AlertDialog will be create first. I think for that solution, I have to use AsyncTask. But in the way I implementing it, I have got some problem regarding using AsyncTask

Below show when the application without AsyncTask forr ip

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set the ip address
        ip = "124.23.204.135";

        //asyncTask for updating gamer's information
        new GamerWorker().execute(ip);

        //refreshing GUI
        intent = new Intent(this, BroadcastService.class);

I'm using AsyncTask GamerWorker() to updating the information from WebService. I also declaring intent for refreshing GUI. Below show when I implemented AsyncTask for AlertDialog

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set the ip address
        LayoutInflater inflater = getLayoutInflater();
        final View dialoglayout = inflater.inflate(
                R.layout.alertdialog_add_gamer, null);
        // start creating the dialog message
        Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // get the editText from the dialog's view
                        EditText text = (EditText) dialoglayout
                                .findViewById(R.id.et_gamerIpOrWebAdress);
                        // disable all input views
                        ip = text.getText().toString();

                    }
                });
        builder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // cancels the dialog
                        dialog.dismiss();
                    }
                });
        new MyTask(builder).execute();
        //refreshing GUI
        intent = new Intent(this, BroadcastService.class);
    }

And for AsyncTask that I have used I declared:

public class MyTask extends AsyncTask<Void, Void, Void> {
          private Builder builder;

        public MyTask(Builder builder) {
            this.builder = builder;
          }

          public void onPreExecute() {
             AlertDialog dialog = builder.create();
            dialog.show();
          }

          public Void doInBackground(Void... unused) {
            return null;
          }

          public void onPostExecute(Void unUsed) {
            new GamerWorker().execute(ip);
          }
        }

Actually from AlertDialog, I'm following this answer: How to display progress dialog before starting an activity in Android? My problem is when I put:

         //refreshing GUI
        intent = new Intent(this, BroadcastService.class);

in preExecute is not working, but if I put in onCreate, it will get null pointer. Anybody knows how to solve this problem?So that I can use AlertDialog when start the application

EDIT: I can put the intent in preExecute, just change the intent to intent = new Intent(MainActivity.this, BroadcastService.class);. But it seems can't solve the problem. The dialog never been created and always got null pointer.Anybody knows?

Community
  • 1
  • 1
eng
  • 83
  • 1
  • 9

1 Answers1

0

For now I solve without using AsyncTask. So I have just declared in onCreate() and then with If-statement I put if the ip is null then the dialog will be shown. If the condition the user has put the Ip it will execute the AsyncTask for updating information to WebService as I mentioned before.

       ip = "";
        // start creating the dialog message
        builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // get the editText from the dialog's view
                        EditText text = (EditText) dialoglayout
                                .findViewById(R.id.et_gamerIpOrWebAdress);
                        // disable all input views
                        ip = text.getText().toString();
                        if (!ip.equals(""))
                        {
                            new GamerWorker().execute(ip);

                            //refreshing GUI
                            intent = new Intent(MainActivity.this, BroadcastService.class);
                        }
                        //clear();
                        //myList.clear();   

                    }
                });
        builder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // cancels the dialog
                        dialog.dismiss();
                    }
                });

        if (ip.equals("") && ip.isEmpty())
        {
            AlertDialog dialog = builder.create();
            dialog.show();

        }
        else
        {
        // set the ip address
            //ip = "123.123.224.133";
            new GamerWorker().execute(ip);
            intent = new Intent(MainActivity.this, BroadcastService.class);
        }
    }

If someone has another solution using AsyncTask, please let me know. Thanks

Ibungo
  • 1,137
  • 12
  • 23
eng
  • 83
  • 1
  • 9