title: Changing a icon in ActionBar depending on a condition
body: <p>I am updating a record in a SQLite database when the user presses an icon on the <code>ActionBar</code>. The information being updated is a flag that adds a record to a Favourites page.</p>
  
  <p>PROBLEM</p>
  
  <p>When the user adds or removes the record to favourites, I would like the icon in the <code>ActionBar</code> to change. I have a full star icon and a empty star icon.</p>
  
  <p>The <code>setIcon</code> method displays the the full star icon if the record is a favourite, and a empty star icon if the record is not a favourite.</p>
  
  <p>In the code below you will see I am using a <code>boolean isInFavourite</code>, which is <code>true</code> when <code>String fav = "y"</code>.</p>
  
  <p>When entering the <code>Activity</code>, the <code>icon</code> displayed is correct. </p>
  
  <p>When the user clicks on the <code>icon</code> to invoke the <code>onMenuItemClick()</code> method, the record is successfully updated but the <code>icon</code> does not change. </p>
  
  <p>I am unable to change the <code>boolean isInFavourite</code> when the record has been updated because Eclipse wants all the variables to be set as <code>final</code></p>
  
  <p>Can anyone help me change the icon to once the record has been updated.</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      db = new DBHelper(this);
      db.createDataBase();
      db.openDataBase();
  
  
      Bundle bundle = getIntent().getExtras();
      final String rowid = bundle.getString("id");
      final String fav = bundle.getString("fav");
  
      //Boolean to check if record is a favourite
      final boolean isInFavourite = fav.contentEquals("y");
  
      menu.add("Add to Favourites")
              .setOnMenuItemClickListener(new OnMenuItemClickListener() {
  
                  public boolean onMenuItemClick(MenuItem item) {
  
                      String toggle;
                      String message;
  
                      //Logic to add or remove row from recording.
                      if (isInFavourite) {
                          toggle = "n";
                          message = "Recipe removed from Favourites";
                      } else {
                          toggle = "y";
                          message = "Recipe added to Favourites";
                      }
  
                      //Update favourite record in database
                      db.updateFavourite(rowid, toggle);
                      db.close();
  
                      Toast.makeText(getApplicationContext(), message,
                              Toast.LENGTH_LONG).show();
  
                      return true;
                  }
  
              })
  
              //Set icon depending on whether record is a favourite or not.
              .setIcon(isInFavourite ? R.drawable.fav_true : R.drawable.fav_false)
              .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
  
      return true;
  
  }
  </code></pre>
  
  <p>Thanks to @dmon for the solution</p>
  
  <p>SOLUTION</p>
  
  <pre><code>private DBHelper db = null;
  
  public String fav = null;
  public String rowid = null;
  public boolean isFav;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      ...
  
      Bundle bundle = getIntent().getExtras();
      rowid = bundle.getString("id");
      fav = bundle.getString("fav");
  
      if (fav.contentEquals("y")) {
          isFav = true;
      } else {
          isFav = false;
      }
  
  
      try {
          db = new DBHelper(this);
          db.createDataBase();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      getSupportMenuInflater().inflate(R.menu.menu_settings, menu);
  
      return true;
  }
  
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
      MenuItem fave = menu.findItem(R.id.add);
      MenuItem unfave = menu.findItem(R.id.remove);
  
      fave.setVisible(isFav);
      unfave.setVisible(!isFav);
  
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case android.R.id.home:
          onBackPressed();
          return true;
      case R.id.add:
          fav = "n";
          isFav = false;
          updateFav();
          supportInvalidateOptionsMenu();
          Toast("Removed from Favourites");
          return true;
      case R.id.remove:
          fav = "y";
          isFav = true;
          updateFav();
          supportInvalidateOptionsMenu();
          Toast("Added to Favourites");
          return true;
      default:
          return super.onOptionsItemSelected(item);
      }
  }
  
  public void updateFav (){
          db.openDataBase();
          db.updateFavourite(rowid, fav);
          db.close();
  }
  </code></pre>
  
  <p>XML File: res/menu/menu_settings.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  &lt;item
      android:id="@+id/add"
      android:icon="@drawable/good"
      android:showAsAction="always"
      /&gt;
  &lt;item
      android:id="@+id/remove"
      android:icon="@drawable/bad"
      android:showAsAction="always"/&gt;
  </code></pre>
  
  <p></p>