コード例 #1
0
  /**
   * This method is called when the user context-clicks a note in the list. NotesList registers
   * itself as the handler for context menus in its ListView (this is done in onCreate()).
   *
   * <p>The only available options are COPY and DELETE.
   *
   * <p>Context-click is equivalent to long-press.
   *
   * @param menu A ContexMenu object to which items should be added.
   * @param view The View for which the context menu is being constructed.
   * @param menuInfo Data associated with view.
   * @throws ClassCastException
   */
  @Override
  public void OnCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {

    // The data from the menu item.
    AdapterView.AdapterContextMenuInfo info;

    // Tries to get the position of the item in the ListView that was long-pressed.
    try {
      // Casts the incoming data object into the type for AdapterView objects.
      info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
      // If the menu object can't be cast, logs an error.
      Log.e(TAG, "bad menuInfo", e);
      return;
    }

    /*
     * Gets the data associated with the item at the selected position. getItem() returns
     * whatever the backing adapter of the ListView has associated with the item. In NotesList,
     * the adapter associated all of the data for a note with its list item. As a result,
     * getItem() returns that data as a Cursor.
     */
    Cursor cursor = (Cursor) getListAdapter().getItem(info.position);

    // If the cursor is empty, then for some reason the adapter can't get the data from the
    // provider, so returns null to the caller.
    if (cursor == null) {
      // For some reason the requested item isn't available, do nothing
      return;
    }

    // Inflate menu from XML resource
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.list_context_menu, menu);

    // Sets the menu header to be the title of the selected note.
    menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));

    // Append to the
    // menu items for any other activities that can do stuff with it
    // as well.  This does a query on the system for any activities that
    // implement the ALTERNATIVE_ACTION for our data, adding a menu item
    // for each one that is found.
    Intent intent =
        new Intent(
            null, Uri.withAppendedPath(getIntent().getData(), Integer.toString((int) info.id)));
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(
        Menu.CATEGORY_ALTERNATIVE,
        0,
        0,
        new ComponentName(this, NotesList.class),
        null,
        intent,
        0,
        null);
  }
コード例 #2
0
  @Override
  public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info;
    try {
      info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
      Log.e(TAG, "bad menuInfo", e);
      return;
    }

    Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
    if (cursor == null) {
      // For some reason the requested item isn't available, do nothing
      return;
    }

    // Inflate menu from XML resource
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.list_context_chunk_menu, menu);

    // Set the context menu header
    menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));

    // Append to the
    // menu items for any other activities that can do stuff with it
    // as well.  This does a query on the system for any activities that
    // implement the ALTERNATIVE_ACTION for our data, adding a menu item
    // for each one that is found.
    Intent intent =
        new Intent(
            null, Uri.withAppendedPath(getIntent().getData(), Integer.toString((int) info.id)));
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(
        Menu.CATEGORY_ALTERNATIVE,
        0,
        0,
        new ComponentName(this, DocumentsEditDetailActivity.class),
        null,
        intent,
        0,
        null);
  }