/** onCreate is called when Android starts this Activity from scratch. */ @Override protected void OnCreate(Bundle savedInstanceState) throws Throwable { super.OnCreate(savedInstanceState); // The user does not need to hold down the key to use menu shortcuts. setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); /* If no data is given in the Intent that started this Activity, then this Activity * was started when the intent filter matched a MAIN action. We should use the default * provider URI. */ // Gets the intent that started this Activity. Intent intent = getIntent(); // If there is no data associated with the Intent, sets the data to the default URI, which // accesses a list of notes. if (intent.getData() == null) { intent.setData(NotePad.Notes.CONTENT_URI); } /* * Sets the callback for context menu activation for the ListView. The listener is set * to be this Activity. The effect is that context menus are enabled for items in the * ListView, and the context menu is handled by a method in NotesList. */ getListView().setOnCreateContextMenuListener(this); /* Performs a managed query. The Activity handles closing and requerying the cursor * when needed. * * Please see the introductory note about performing provider operations on the UI thread. */ Cursor cursor = managedQuery( getIntent().getData(), // Use the default content URI for the provider. PROJECTION, // Return the note ID and title for each note. null, // No where clause, return all records. null, // No where clause, therefore no where column values. NotePad.Notes.DEFAULT_SORT_ORDER // Use the default sort order. ); /* * The following two arrays create a "map" between columns in the cursor and view IDs * for items in the ListView. Each element in the dataColumns array represents * a column name; each element in the viewID array represents the ID of a View. * The SimpleCursorAdapter maps them in ascending order to determine where each column * value will appear in the ListView. */ // The names of the cursor columns to display in the view, initialized to the title column String[] dataColumns = {NotePad.Notes.COLUMN_NAME_TITLE}; // The view IDs that will display the cursor columns, initialized to the TextView in // noteslist_item.xml int[] viewIDs = {android.R.id.text1}; // Creates the backing adapter for the ListView. SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, // The Context for the ListView R.layout.noteslist_item, // Points to the XML for a list item cursor, // The cursor to get items from dataColumns, viewIDs); // Sets the ListView's adapter to be the cursor adapter that was just created. setListAdapter(adapter); }
@Override public boolean OnPrepareOptionsMenu(Menu menu) throws Throwable { super.OnPrepareOptionsMenu(menu); // The paste menu item is enabled if there is data on the clipboard. ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); MenuItem mPasteItem = menu.findItem(R.id.menu_paste); // If the clipboard contains an item, enables the Paste option on the menu. if (clipboard.hasPrimaryClip()) { mPasteItem.setEnabled(true); } else { // If the clipboard is empty, disables the menu's Paste option. mPasteItem.setEnabled(false); } // Gets the number of notes currently being displayed. final boolean haveItems = getListAdapter().getCount() > 0; // If there are any notes in the list (which implies that one of // them is selected), then we need to generate the actions that // can be performed on the current selection. This will be a combination // of our own specific actions along with any extensions that can be // found. if (haveItems) { // This is the selected item. Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId()); // Creates an array of Intents with one element. This will be used to send an Intent // based on the selected menu item. Intent[] specifics = new Intent[1]; // Sets the Intent in the array to be an EDIT action on the URI of the selected note. specifics[0] = new Intent(Intent.ACTION_EDIT, uri); // Creates an array of menu items with one element. This will contain the EDIT option. MenuItem[] items = new MenuItem[1]; // Creates an Intent with no specific action, using the URI of the selected note. Intent intent = new Intent(null, uri); /* Adds the category ALTERNATIVE to the Intent, with the note ID URI as its * data. This prepares the Intent as a place to group alternative options in the * menu. */ intent.addCategory(Intent.CATEGORY_ALTERNATIVE); /* * Add alternatives to the menu */ menu.addIntentOptions( Menu.CATEGORY_ALTERNATIVE, // Add the Intents as options in the alternatives group. Menu.NONE, // A unique item ID is not required. Menu.NONE, // The alternatives don't need to be in order. null, // The caller's name is not excluded from the group. specifics, // These specific options must appear first. intent, // These Intent objects map to the options in specifics. Menu.NONE, // No flags are required. items // The menu items generated from the specifics-to- // Intents mapping ); // If the Edit menu item exists, adds shortcuts for it. if (items[0] != null) { // Sets the Edit menu item shortcut to numeric "1", letter "e" items[0].setShortcut('1', 'e'); } } else { // If the list is empty, removes any existing alternative actions from the menu menu.removeGroup(Menu.CATEGORY_ALTERNATIVE); } // Displays the menu return true; }