0

My Alert dialog message doesn't come out because I cannot get the text of the button. After that the program will directly go to my EventActivity. How can settle this problem?

public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == insertButton.getId()) {

            if(colorButton.getText().toString().equals("Color")){
                colorAlert.show();
            }
        }
}

This is variable

AlertDialog colorAlert;

AlertDialog in the OnCreate()

AlertDialog.Builder CA = new AlertDialog.Builder(this);
        CA.setTitle("Alert Message!");
        CA.setMessage("Please insert the level of important for the event.");
        CA.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                colorAlert.dismiss();
                startActivity(new Intent(this, EventActivity.class));
            }
        });
        colorAlert = CA.create();
user2274349
  • 415
  • 2
  • 5
  • 12

3 Answers3

3

When you compare a String in Java, use YOUR_STRING.equals("Color");

Update: change your while into if , because your are running it once.

The move your

startActivity(new Intent(this, EventActivity.class));

in your alertbox positive button handler.

Comparing Strings in Java.

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • that's mean I need to have my code is `colorButton.getText().toString().equals("Color")`? If like this then it is stuck at there. – user2274349 Apr 26 '13 at 16:32
  • @user2274349 updated. Change while into if. You should post code for your alertbox if you still need help on the next step. – wtsang02 Apr 26 '13 at 16:33
  • Again... `startActivity(new Intent(this, EventActivity.class));` error if I put in my alertbox there. – user2274349 Apr 26 '13 at 16:42
  • done edit...the error is in the `startActivity(new Intent(this, EventActivity.class));` – user2274349 Apr 26 '13 at 16:56
  • `colorButton.getText().toString().equals( "Color");` change `==` to `.equals("Color");` If you still get an error post Error/logcat/stacktrace. – wtsang02 Apr 26 '13 at 16:59
  • it is exactly cannot run. Because the error still at there. `startActivity(new Intent(this, EventActivity.class));` this error cannot fix then the alert shown "Please fix the error before run." – user2274349 Apr 26 '13 at 17:09
  • use `YourCurrentClassName.this.startActivity(new Intent(this, EventActivity.class));` – wtsang02 Apr 26 '13 at 17:28
0

Adding to wtsang02 answer, you shouldn't put your alert.show in a while like this one... I see coming the infinite loop ;)

TheBeps
  • 36
  • 4
0

Ok look like you need more help, here is one solution... i let you deal with the EventActivity ;)

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;


public class MainActivity extends Activity {

    final Context context=this;
    public static String EXTRA_MESSAGE;
    Button colorButton;


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

        colorButton = (Button) findViewById(R.id.button1);

        colorButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                if(colorButton.getText().toString().equals("Color")){


                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                    alertBuilder.setMessage("myDialog").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            Intent intent = new Intent(MainActivity.this, EventActivity.class);
                            String message = colorButton.getText().toString();
                            intent.putExtra(EXTRA_MESSAGE, message);
                            startActivity(intent);
                            }
                    }).setNegativeButton("cancel", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            }
                    });

                    AlertDialog myAlert = alertBuilder.create();
                    myAlert.show();

                }   

            }
        });


    }
TheBeps
  • 36
  • 4