1

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.

1 Answer 1

1

You have to decide how to handle a situation when both kind of items are selected at once (say, one with true in db and one with false). 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 with true in the database is selected and decrease it whenever such item is deselected. If it's zero in onPrepareActionMode - don't show your button.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking of something similar using the ids of the fields set to true but this is much better. Thank you Vladimir :). To those who might find this question useful, don't forget to set the counter to 0 when the CAB is destroyed otherwise if the user selects an item with the DB field set to true and pressed the back button the counter will start from 1 on the next select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.