Results 1 to 5 of 5
  1. #1
    ics1010 is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default scoping variable question

    If you look at the code...
    Java Code:
            exit.setOnClickListener(new OnClickListener() {
            	@Override
            	public void onClick(View v) {
            		SFMainMenu.this.view = v;
            		// end game
            		AlertDialog.Builder builder = new AlertDialog.Builder(SFMainMenu.this);
            		builder.setMessage(R.string.exit_message)
            		.setCancelable(false)
            		.setPositiveButton(R.string.yes_string, new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int id) {
    		        		boolean clean = false;
    		        		clean = engine.onExit(SFMainMenu.this.view);
    		        		if(clean) {
    		        			int pid = android.os.Process.myPid();
    		        			android.os.Process.killProcess(pid);
    		        		}
    					}
    				})
    				.setNegativeButton(R.string.no_string, new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int id) {
    						; // do nothing
    					}
    				});
            		builder.show();
            	}
            });
    I've made a class variable to hold the View v passed into the containing onClick override to be able to have it available to the inner dialog's positive button onClick block. Is there a different way to scope this to have it available to the inner block without having to use the class variable? (I hope that makes sense =P)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: scoping variable question

    You can use a final variable:

    Java Code:
    final View view = v;
    
    // later
    builder.setPositiveButton(R.string.yes_string, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // use view here
        }
    );

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: scoping variable question

    Quote Originally Posted by ics1010 View Post
    If you look at the code...
    Is that Java or Android code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ics1010 is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: scoping variable question

    db, it's running on android but since I was asking java specific, I figured this would be the right place. pbrockway2, thanks for the help kiwi =)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: scoping variable question

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2012, 07:46 AM
  2. Question about Variable Scope
    By Solarsonic in forum New To Java
    Replies: 2
    Last Post: 03-17-2011, 06:15 AM
  3. Elementary question on variable references
    By Shayke_ in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 04:35 PM
  4. Replies: 0
    Last Post: 03-31-2008, 10:06 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •