I’m trying to show an extra menu item (contextual action bar) when the user selects one/multiple items in a ListView. Basically I’m checking if the selected item has a field in the DB set to true. If so, I add the extra menu item.
I tried to query the DB for the selected item in onItemCheckedStateChanged() and invalidate the menu. This will call onPrepareActionMode() where I could show the extra option if needed.
This does not work because the options that are already selected with the DB column set to true, are not remembered when an item with the special DB column set to false gets selected in onItemCheckedStateChanged() and when onPrepareActionMode() gets called the extra menu item is removed.
How can I approach this better? Essentially I need a way to remember all the selected ListView items that have the extra field in the DB set to true so I can show the extra menu item – and vice-versa remove the item when none of the selected ListView items has that DB field.
You have to decide how to handle a situation when both kind of items are selected at once (say, one with
truein db and one withfalse). If you decide to allow it, track your button state somewhere in your activity. I’d add a private integer member to your activity and increase this number whenever the item withtruein the database is selected and decrease it whenever such item is deselected. If it’s zero inonPrepareActionMode– don’t show your button.