-3

I have been trying to implement this dialog box for the past 3 hours and I can't figure out why it's not popping up, I think it's best to show my entire class to understand my issue:

Registration.java

public class Registration extends Activity {





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






    //on submit press
    public void SubmitRegistration(View view) {



        // start an asynch request
        class RequestTask extends AsyncTask<String, String, String>{





            @Override
            public String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (IOException e) {
                    //TODO Handle problems..
                }
                return responseString;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //Do anything with response..
            }
        }










           // assign text in fields to string values
           EditText first = (EditText)findViewById(R.id.first);
           String first2 = first.getText().toString();

           EditText last = (EditText)findViewById(R.id.last);
           String last2 = last.getText().toString();

           EditText display = (EditText)findViewById(R.id.display);
           String display2 = display.getText().toString();
           //calculates the number of characters in the display field
           int length2 = display2.length();


           EditText email = (EditText)findViewById(R.id.email);
           String email2 = email.getText().toString();


           EditText password = (EditText)findViewById(R.id.password);
           String password2 = password.getText().toString();


           EditText vpassword = (EditText)findViewById(R.id.vpassword);
           String vpassword2 = vpassword.getText().toString();
           //calculates the number of characters in the password field
           int length = vpassword2.length();









           // verifying the following in order:  Passwords match? A Password field is empty?  
           //Password and Display Name less than 6 characters long? Email contains an @ sign and a period?   
           if(!vpassword2.equals(password2)) {
           Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show(); 


           }
           else if (password2.isEmpty() || vpassword2.isEmpty()){

           Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show(); 
           }
           else if (length < 6 || length2 < 6 ) {
           Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show(); 
           }

           else if (!email2.contains("@") || !email2.contains(".")){
           Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show(); 
           }
           //start else
           else {

           //send php with all the data to server for validation and insertion into table
           String output = null;
                try {
                output = new RequestTask()
                .execute("http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
                .get();
                //example: www.alkouri.com/android/registercheck.php?first=Adam&last=Alkouri&display=arugala&email=arugala@blackbaud.com&password=123
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }





                //if the response from website contains "duplicate" (which means there is a duplicate email address in the DB) then it will display toast.
          if (output.contains("Duplicate")) {
          Toast.makeText(getApplicationContext(), "Email address already in system, press back button if you forgot password and click on Forgot Password ", Toast.LENGTH_LONG).show(); 

          }
          //if the response from website contains "You have registered successfully" then it will send popup message and go to login screen 
          else if (output.contains("You have")){




         //start dialogue
         AlertDialog.Builder alertbox = new AlertDialog.Builder(Registration.this);
         alertbox.setMessage("You have succesfully registered.  Please check your email for further instructions."); // Please Restart Application // "Please restart the app and download your purchase again".
         alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            //create button in dialogue
            public void onClick(DialogInterface arg0, int arg1) 
            {
            //on clicking "ok" in the dialogue box, current activity will close and return to last activity (login screen).
            finish();               

            }
         });
         alertbox.show();
          }     
            }//end else







     }  //end button click task
}//end class

So, at the very bottom, directly under the very last toast, I would like a dialogue box to pop up.
It's to let the user know they have registered and they should check their email for further instructions.

The toast is showing up just fine, but when I implement ANY of the following examples, nothing pops up.
I don't get any errors in the LogCat, it's just that nothing pops up....

I have tried the following examples that I Have seen online:

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

http://stackoverflow.com/questions/2115758/how-to-display-alert-dialog-in-android

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

What am I doing wrong?

user3053446
  • 124
  • 2
  • 13

3 Answers3

1
AlertDialog.Builder alertbox = new AlertDialog.Builder(MainMenuActivity.this);
        alertbox.setMessage("Please restart the app and download your data again."); // Please Restart Application // "Please restart the app and download your purchase again".
        alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                    //code
            }
        });

        alertbox.setNegativeButton("NO", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //code  
            }
        });

        alertbox.show();
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0

You need to put an OnclickListener for your submit button in onCreate() method and call your other functions from there.

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //Call your submitregistration function function here                
        }
    });
}
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
0
 output = new RequestTask()
.execute("http://www.mywebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
.get();

Calling get does not make it asynchronous anymore. It blocks the ui thread waiting for the result.

Move the AsyncTask out of onClick

class RequestTask extends AsyncTask<String, String, String>{

http://developer.android.com/reference/android/os/AsyncTask.html#get()

public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

Also Declare your variables a instance variables and initialize your views in onCreate.

EditText first ;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
first = (EditText)findViewById(R.id.first);

On Button click get the text from editText

Invoke asynctask

 new RequestTask().execute(params); // no get

The result of doInbackground computation is a param to onPostExecute. Based on the result you can display dialog in onPostExecute.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256