Inside of my Activity class I have declared three important variables:
What each variable tracks should be self explanatory from the declaration, but please comment if you have questions on anything in my posts. The reason it's important to declare these within scope of the Activity class and not within a method is because we lose focus of what ListView item is being interacted with in-between the step of creating a menu and when that menu is used. There might be a way to extract that information from the second step, but none was obvious to me and I found the approach of using private variables an easy solution.
In addition to those three variables I've also created constant values to track the int-based menu options:
These allow me to refer to actions in a clear way in the code. In the OnCreate method of my Activity there are two important steps needed to use context menus:
Now that I've associated my ListView with a private variable and have registered it for ContextMenus I only have to define two additional methods for everything to work. The first is the method that is triggered when an item is selected because of the RegisterForContextMenu call made in OnCreate, and it creates the menu that is displayed:
Okay, so now my menu will show up and it will look different based on what text the item in the ListView contains, and what item number it is assigned, which is the 0-based index in the listview. You can see in my menu code above that I have added the ability to View to any-and-all items, but only the first item in the list can be edited, and only items containing my special text can be deleted.
The last step is to specify what actions occur when the menu is used. We do this in the OnContextItemSelected method:
This is where the information captured as selectedItemText and selectedItemNumber from OnCreateContextMenu comes in handy. Since this method only has access to the item that was clicked, and that IMenuItem contains no information about the ListView item we have to refer to our Activity-scope variables selectedItemText and selectedItemNumber.
As always, feel free to post questions or comments, even if it's to suggest a better way of doing this!
No comments:
Post a Comment