title: Need to accest variable in a setOnClickListener method
body: <p>I want that variable r to be global and I want SharedPreferences to be able to load variable r (String s) on global level.</p>
  
  <p>What I'm making is something like a artificial robot HUNGER variable. I click on button and it adds +1 hunger, but every second I want to give it -1 hunger with timestamp. Also timestamp serves as indicator how long has device been offline and it gives -xx proportionaly. </p>
  
  <p>As of right now, I can only add +1 to R since its local variable in onClickListener, I can't give it if( time has pased){ r = r-1 } because r is localy declared. I dont know how to get it out.</p>
  
  <pre><code>import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.os.Handler;
  import android.preference.PreferenceManager;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
      private TextView broj;
      private TextView tekst1;
      private TextView posljednjeVrijeme;
      String stringTStamp;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
                  /**
                   * Ponavlja radnju svakih 4 sekunde, učitava vrijeme systema
                   */
                          final Handler ha=new Handler();
                          ha.postDelayed(new Runnable() {
  
                              @Override
                              public void run() {
                                  Long tStamp = System.currentTimeMillis()/1000;
                                  stringTStamp = tStamp.toString();
                                  Toast.makeText(MainActivity.this, stringTStamp, Toast.LENGTH_SHORT).show();
                                  SharedPreferences preferences3 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                                  SharedPreferences.Editor editor = preferences3.edit();
                                  editor.putString("posljednjeVrijeme", stringTStamp);
                                  editor.apply();
  
  
  
                                  ha.postDelayed(this, 4000);
                              }
                          }, 4000);
                  /**
                   * **************************************************************
                   */
  
  
  
          broj = (TextView)findViewById(R.id.broj);
          posljednjeVrijeme = (TextView)findViewById(R.id.posljednjeVrijeme);
  
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
          final String name = preferences.getString("vrijednost je ", "");
          final String name2 = preferences.getString("posljednjeVrijeme", "");
          if(!name.equalsIgnoreCase(""))
          {
  
              broj.setText("vrijednost je "+name);
              posljednjeVrijeme.setText("Zadnje vrijeme "+name2);
          }
          Long tStamp = System.currentTimeMillis()/1000;
          stringTStamp = tStamp.toString();
          if(Integer.parseInt(stringTStamp)-Integer.parseInt(name2)&gt;10){
              Toast.makeText(this, "POZDRAV GAZDA DUGO SE NE VIDJEH, UGASIO SI ME PRIJE " + (Integer.parseInt(stringTStamp)-Integer.parseInt(name2))+ " SEKUNDI" , Toast.LENGTH_SHORT).show();
  
  
          }else{
  
          }
  
  
          tekst1 = (TextView)findViewById(R.id.tekst1);
          final SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(this);
              tekst1.setOnClickListener(new View.OnClickListener() {
                  int r = 0+Integer.parseInt(name);
                  @Override
                  public void onClick(View v) {
  
                      r = r+1;
  
  
                      broj.setText("vrijednost je "+r);
  
                      String s= String.valueOf(r);
                      SharedPreferences.Editor editor = preferences2.edit();
                      editor.putString("vrijednost je ", s);
                      editor.apply();
                  }
              });
  
      }
  }
  </code></pre>