Handling variables outside of a block
===

I have an application set up to where there are eighteen buttons. The basic idea behind this is that if three of those buttons are clicked, a string variable is assigned to each button. The values are then added together and compared to a string. If the values of the strings match, I  would like an alert dialog to appear. I tried storing each button variable in shared preferences, but the alert dialog shows only after you restart that activity. I would like this to happen instantly. If anyone can shed any light on this, I would be forever grateful. Also, if a more efficient way of doing this is possible, that would be helpful. 

I am not sure hot to handle variables outside of a block such as a button. If I knew how to, I would just pass the three variables outside of the button block and call for it within the same file, compare and then set the alert. Again, the basic logic is: if three specific buttons are selected and all three are selected, then and only then should the alert dialog appear. (If it helps, any other button selected resets all the other buttons). Code:


    

    public class Stage1Level1Activity extends Activity {
	
	
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stage1level1);
        
      
        
        ImageButton a1 = (ImageButton) findViewById(R.id.a1);
        ImageButton a3 = (ImageButton) findViewById(R.id.a3);
        ImageButton a4 = (ImageButton) findViewById(R.id.a4);
        ImageButton b1 = (ImageButton) findViewById(R.id.b1);
        final ImageButton b2 = (ImageButton) findViewById(R.id.b2);
        ImageButton b3 = (ImageButton) findViewById(R.id.b3);
        ImageButton b4 = (ImageButton) findViewById(R.id.b4);
        ImageButton c1 = (ImageButton) findViewById(R.id.c1);
        final ImageButton c2 = (ImageButton) findViewById(R.id.c2);
        ImageButton c3 = (ImageButton) findViewById(R.id.c3);
        ImageButton c4 = (ImageButton) findViewById(R.id.c4);
        ImageButton d1 = (ImageButton) findViewById(R.id.d1);
        final ImageButton d2 = (ImageButton) findViewById(R.id.d2);
        ImageButton d3 = (ImageButton) findViewById(R.id.d3);
        ImageButton d4 = (ImageButton) findViewById(R.id.d4);
        ImageButton e1 = (ImageButton) findViewById(R.id.e1);
        ImageButton e3 = (ImageButton) findViewById(R.id.e3);
        ImageButton e4 = (ImageButton) findViewById(R.id.e4);
        
    
        
        a1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            
               
            }
        	});
       
        
        a3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        a4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        
        b2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_filled);
            	SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences1.edit();
                editor.putString("part1", "a");
                editor.commit();
            }
        	});
        b3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        b4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        c1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        c2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	c2.setBackgroundResource(R.drawable.green_filled);
            	SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences2.edit();
                editor.putString("part2", "b");
                editor.commit();
            }
        	});
        c3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        c4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        d1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        d2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	d2.setBackgroundResource(R.drawable.green_filled);
            	SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences3.edit();
                editor.putString("part3", "c");
                editor.commit();
            }
        	});
        d3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        d4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        e1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        
        e3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        e4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	b2.setBackgroundResource(R.drawable.green_empty);
            	c2.setBackgroundResource(R.drawable.green_empty);
            	d2.setBackgroundResource(R.drawable.green_empty);
            	
               
            }
        	});
        
        SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        String part1 = sharedPreferences1.getString("part1", "");
        SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        String part2 = sharedPreferences2.getString("part2", "");
        SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        String part3 = sharedPreferences3.getString("part3", "");
   
        String added = part1 + part2 + part3;
        String compared = "abc";
        
        if (added.equalsIgnoreCase(compared) ){
        	
        	AlertDialog alertDialog = new AlertDialog.Builder(Stage1Level1Activity.this).create();
        	alertDialog.setTitle("Reset...");
        	alertDialog.setMessage("R u sure?");
        	alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
        	      public void onClick(DialogInterface dialog, int which) {
        	 
        	       
        	 
        	    } });
        	
        	alertDialog.show();
        }
        
        
      
        
       
    }
    }












