Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:25:48+00:00 2026-06-09T07:25:48+00:00

I am updating a record in a SQLite database when the user presses an

  • 0

I am updating a record in a SQLite database when the user presses an icon on the ActionBar. The information being updated is a flag that adds a record to a Favourites page.

PROBLEM

When the user adds or removes the record to favourites, I would like the icon in the ActionBar to change. I have a full star icon and a empty star icon.

The setIcon 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.

In the code below you will see I am using a boolean isInFavourite, which is true when String fav = "y".

When entering the Activity, the icon displayed is correct.

When the user clicks on the icon to invoke the onMenuItemClick() method, the record is successfully updated but the icon does not change.

I am unable to change the boolean isInFavourite when the record has been updated because Eclipse wants all the variables to be set as final

Can anyone help me change the icon to once the record has been updated.

@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;

}

Thanks to @dmon for the solution

SOLUTION

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();
}

XML File: res/menu/menu_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/add"
    android:icon="@drawable/good"
    android:showAsAction="always"
    />
<item
    android:id="@+id/remove"
    android:icon="@drawable/bad"
    android:showAsAction="always"/>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:25:52+00:00Added an answer on June 9, 2026 at 7:25 am

    The easiest way is to just provide two different buttons and hide/show them accordingly:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inf = new MenuInflater(this);
      inf.inflate(R.menu.menu_xml, menu);
      return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
      MenuItem fave = menu.findItem(R.id.favorite);
      MenuItem unfave = menu.findItem(R.id.unfavorite);
    
      fave.setVisible(!isFave);
      unfave.setVisible(isFave);
      return true;
    }
    

    Then you invalidate the options menu when the state has changed. Note that you have to have a global variable that has the current state of the item (where isFave is coming from)

    invalidateOptionsMenu();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a process that is updating a record and encounters a record
Below is my query which is updating a record in the User table ,
For updating record in Entity framework I am trying that. I am just assigning
i am updating record through jquery, and once data is updated how can one
I have a text field in SQLite database, which I want the user to
About best practices on designing stored procedures, should a stored procedure updating a record
Have a problem with updating an existing record of my DB. In my view
I made iPad application in which, I want to insert record into database table,
We are using Symfony2 to create an API. When updating a record, we expect
Suppose that I have a huge SQLite file (say, 500[MB]). Can 10 different python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.