Download presentation
Presentation is loading. Please wait.
Published bySamantha Bryant Modified over 9 years ago
1
Copyright© Jeffrey Jongko, Ateneo de Manila University Editing ListAdapter Data part 2
2
Selection and Touch Selection and Touch are two different interaction systems in Android Selection is used when using Arrows/Fire keys Touch is used when you tap using a touch screen These two are independent of each other It is a major UI design issue to be able to handle both cleanly Usually, focuses on Touch since many phones no longer have arrow/fire keys
3
Example When interaction with a ListView you can do the following Use the arrow keys to move around and use the fire key to select the row Tap the ListView and row directly For now, we will focus on the use of Touch
4
Copyright© Jeffrey Jongko, Ateneo de Manila University Context Sensitive Menus
5
Context Sensitive Menus are menus whose contents are dependent on the selected item (the context) The Activity class is responsible for the creation of these menus and provides methods that need to be overridden (just like the Options menu)
6
General Steps To enable Context Menus you need to do the following Register which view needs context menus Override onCreateContextMenu() Override onContextItemSelected()
7
registerForContextMenu() For a view to have a context menu, it must be registered to the activity using registerForContextMenu(View) Any number of views can have context menus, just register them all
8
Example ListView lv = getListView(); lv.setTextFilterEnabled(true); // NOTE: REGISTER listview for context menu registerForContextMenu(lv);
9
onCreateContextMenu() Works similar to the onCreateOptionsMenu() Use a MenuInflater to initialize the menu based on an XML file Alternatively you can invoke the menu’s add() method to add options programatically E.g. for dynamic menus
10
Example @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); // places the contents of the XML to the menu inflater.inflate(R.menu.context_menu, menu); }
11
Example XML <menu xmlns:android="http://schemas.android.com/apk/res/android">
12
onContextItemSelected() To handle selections override onContextItemSelected(MenuItem item) Switch against the menuItem’s ID to determine which action was selected Make sure to propagate the call to the superclass should none of the cases handle the option
13
AdapterContextMenuInfo The MenuItem parameter contains a ContextMenuInfo object retrieved from using getMenuInfo() For a ListView, it returns an AdapterContextMenuInfo instance The main job of this class is to pass back the following target view – which specific view this menu is for position of the view within the targetView This will allow you to extract the specific object within the backing data id of the view within the targetView
14
Example @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.delete: // delete the currently selected row // NOTE: trigger the changes sampleList.remove(info.position); adapter.notifyDataSetChanged(); return true; default: return super.onContextItemSelected(item); }
15
Extras Other menu features Submenus MenuGroups Checkable menus Shortcut keys Check out file:///C:/android-sdk- windows/docs/guide/topics/ui/menus.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.