Sourav Roy
Sourav Roy

Reputation: 323

Accessing a global variable from an onClickListener inside an alertDialog

enter image description here

In the marked line str asking me to set str as a local variable

public void alertShow(String s){
    //Game Ended...!!!
    android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(this);

    // Setting Dialog Title
    if(s.equals("You") || (s.contains("P")))
        alertDialog.setTitle("Congratulations...");
    else
        alertDialog.setTitle("Game's Up...");

    // Setting Dialog Message
    if(s.equals("You") || s.contains("P"))
        alertDialog.setMessage(s+" Win The Game...");
    else
        alertDialog.setMessage("You Lose... I Win...");

    final ImageView img = new ImageView(this);
    if(s.equals("You") || (s.contains("P")))
        img.setImageResource(R.drawable.firecrackers);
    else
        img.setImageResource(R.drawable.mmm);

    alertDialog.setView(img);

    // Setting Dialog Cancellation
    alertDialog.setCancelable(false);

    // Setting Positive "Yes" Button

    //==================Here Problem Starts==========================

    SessionClass sessionClass = new SessionClass();
    final String str = sessionClass.getPlayer();
    alertDialog.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent i;
            if (str.contains("1"))
                i = new Intent(MainActivity2.this, Toss.class);
            else
                i = new Intent(MainActivity2.this, Game2.class);
            startActivity(i);
            finish();
        }
    });

    //=====================================================

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent i = new Intent(MainActivity2.this, MainActivity2.class);
            startActivity(i);
            finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

I want to access str from onClickListener which is declared outside onClickListener. I have tried the following code but it is not accessing the variable.

Upvotes: 0

Views: 1429

Answers (3)

bastami82
bastami82

Reputation: 6153

Final , should let you access your variable inside your AlertDialoge , can you confirm that you actually getting string from this method (getPlayer();)?

the reason I am asking this is (you are not checking for null) and if that method returns null then you will get error when you want to check if null has "1".

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

From docs of annonymous inner classes

Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class

Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:

An anonymous class has access to the members of its enclosing class.

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.

Mark your string as final, then you can use it.

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 2073

You need to mark the str variable as final, and then you can access it from onClickListener

Upvotes: 2

Related Questions