dimanche 22 novembre 2015

How can you update a checkbox in a MenuItem dynamically?

This is my layout for a menu.

Notice the item:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd">
    <item android:id="@+id/action_best_stores"
        android:title="@string/showStores"
        android:onClick="showHideStores"
        android:enabled="true"
        android:checkable="true"
        app:showAsAction="never"
        />
</menu>

This is the onAction function.

public void showHideStores(MenuItem m) {
    m.setChecked(!m.isChecked());
    SHOWING_STORES = !SHOWING_STORES;
    showStoresProcess(SHOWING_STORES);
    refreshData();
    invalidateOptionsMenu();

}

Whenever I run my program, the function works because things that are supposed to happen, happen. Except, for the part where the ItemMenu itself changes... It just doesn't do anything, the checkbox stays unchecked... I also tried setTitle, doesn't change anything either.

So, line 1 isn't producing any change.

What is going on?

Notice how the function works because the screen changes, but the menu itself doesn't get "checked off" or "unchecked".

enter image description here

enter image description here




Aucun commentaire:

Enregistrer un commentaire