Context Menu Frustration

Put your problem here if it does not fit any of the other categories.

Context Menu Frustration

Postby snappy » Fri Dec 12, 2008 3:45 am

Hi guys, I'm new to android development.

I'm extending the notepad application that is in the tutorial at googles site
by adding a context menu to delete an item, and eventually be able to
share the item over email (and messaging).

The problem is onContextItemSelected never fires, unless I set the menu
ID of the menu items to 0 (which will make it really difficult to have multiple
menu items in the context menu as you can imagine).

The changes I made in NotepadV3 were in the fillData() method where
I unregister and register context menu listeners. And I added the two
methods for handling context menu listeners at the bottom of the class.

I littered the code with log output and this is how I know that
onContextItemSelected is not firing. However, when creating
the context menu I see the output (from logcat):

Code: Select all
V/Notepadv3(  394): Created context menu.
V/Notepadv3(  394): Added menu item: 2
V/Notepadv3(  394): Added menu item: 1



Please inspect the code below and tell me what I might be missing.

Any assistance would be appreciated.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /*
  2.  
  3.  * Copyright (C) 2008 Google Inc.
  4.  
  5.  *
  6.  
  7.  * Licensed under the Apache License, Version 2.0 (the "License")savedInstanceState;
  8.  
  9.  * you may not use this file except in compliance with the License.
  10.  
  11.  * You may obtain a copy of the License at
  12.  
  13.  *
  14.  
  15.  *      http://www.apache.org/licenses/LICENSE-2.0
  16.  
  17.  *
  18.  
  19.  * Unless required by applicable law or agreed to in writing, software
  20.  
  21.  * distributed under the License is distributed on an "AS IS" BASIS,
  22.  
  23.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24.  
  25.  * See the License for the specific language governing permissions and
  26.  
  27.  * limitations under the License.
  28.  
  29.  */
  30.  
  31.  
  32.  
  33. package com.android.demo.notepad3;
  34.  
  35.  
  36.  
  37. import android.app.ListActivity;
  38.  
  39. import android.content.Intent;
  40.  
  41. import android.database.Cursor;
  42.  
  43. import android.os.Bundle;
  44.  
  45. import android.util.Log;
  46.  
  47. import android.view.ContextMenu;
  48.  
  49. import android.view.Menu;
  50.  
  51. import android.view.MenuItem;
  52.  
  53. import android.view.View;
  54.  
  55. import android.widget.ListView;
  56.  
  57. import android.widget.SimpleCursorAdapter;
  58.  
  59.  
  60.  
  61. public class Notepadv3 extends ListActivity {
  62.  
  63.     private static final int ACTIVITY_CREATE=0;
  64.  
  65.     private static final int ACTIVITY_EDIT=1;
  66.  
  67.    
  68.  
  69.     private static final int INSERT_ID = Menu.FIRST;
  70.  
  71.     private static final int DELETE_ID = Menu.FIRST + 1;
  72.  
  73.    
  74.  
  75.     private static final int CONTEXT_SHARE_ID = Menu.FIRST;
  76.  
  77.     private static final int CONTEXT_DELETE_ID = Menu.FIRST+1;
  78.  
  79.  
  80.  
  81.     private static final String TAG = "Notepadv3";
  82.  
  83.    
  84.  
  85.     private NotesDbAdapter mDbHelper;
  86.  
  87.    
  88.  
  89.     /** Called when the activity is first created. */
  90.  
  91.     @Override
  92.  
  93.     public void onCreate(Bundle savedInstanceState) {
  94.  
  95.         super.onCreate(savedInstanceState);
  96.  
  97.         Log.e(TAG, TAG+" started!");
  98.  
  99.         setContentView(R.layout.notes_list);
  100.  
  101.         mDbHelper = new NotesDbAdapter(this);
  102.  
  103.         mDbHelper.open();
  104.  
  105.         fillData();
  106.  
  107.     }
  108.  
  109.    
  110.  
  111.     private void fillData() {
  112.  
  113.         unregisterForContextMenu(getListView());
  114.  
  115.        
  116.  
  117.         // Get all of the rows from the database and create the item list
  118.  
  119.         Cursor notesCursor = mDbHelper.fetchAllNotes();
  120.  
  121.         startManagingCursor(notesCursor);
  122.  
  123.        
  124.  
  125.         // Create an array to specify the fields we want to display in the list (only TITLE)
  126.  
  127.         String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
  128.  
  129.        
  130.  
  131.         // and an array of the fields we want to bind those fields to (in this case just text1)
  132.  
  133.         int[] to = new int[]{R.id.text1};
  134.  
  135.        
  136.  
  137.         // Now create a simple cursor adapter and set it to display
  138.  
  139.         SimpleCursorAdapter notes =
  140.  
  141.                     new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
  142.  
  143.         setListAdapter(notes);
  144.  
  145.         registerForContextMenu(getListView());
  146.  
  147.     }
  148.  
  149.    
  150.  
  151.     @Override
  152.  
  153.     public boolean onCreateOptionsMenu(Menu menu) {
  154.  
  155.         super.onCreateOptionsMenu(menu);
  156.  
  157.         menu.add(Menu.NONE, INSERT_ID, Menu.NONE, R.string.menu_insert);
  158.  
  159.         menu.add(Menu.NONE, DELETE_ID, Menu.NONE, R.string.menu_delete);
  160.  
  161.         return true;
  162.  
  163.     }
  164.  
  165.  
  166.  
  167.     @Override
  168.  
  169.     public boolean onMenuItemSelected(int featureId, MenuItem item) {
  170.  
  171.         switch(item.getItemId()) {
  172.  
  173.         case INSERT_ID:
  174.  
  175.             createNote();
  176.  
  177.             return true;
  178.  
  179.         case DELETE_ID:
  180.  
  181.             mDbHelper.deleteNote(getListView().getSelectedItemId());
  182.  
  183.             fillData();
  184.  
  185.             return true;
  186.  
  187.         }
  188.  
  189.        
  190.  
  191.         return super.onMenuItemSelected(featureId, item);
  192.  
  193.     }
  194.  
  195.  
  196.  
  197.     @Override
  198.  
  199.     public boolean onContextItemSelected(MenuItem item)
  200.  
  201.     {
  202.  
  203.         Log.v(TAG, "menu item id selected = "+item.getItemId());
  204.  
  205.         Log.v(TAG, "List View id selected = "+getListView().getSelectedItemId());
  206.  
  207.        
  208.  
  209.         switch(item.getItemId()) {
  210.  
  211.         case CONTEXT_SHARE_ID:
  212.  
  213.                 Log.v(TAG, "TESTING");
  214.  
  215.                 return true;
  216.  
  217.        
  218.  
  219.         case CONTEXT_DELETE_ID:
  220.  
  221.                 mDbHelper.deleteNote(getListView().getSelectedItemId());
  222.  
  223.                 fillData();
  224.  
  225.                 return true;
  226.  
  227.         }
  228.  
  229.        
  230.  
  231.         return super.onContextItemSelected(item);
  232.  
  233.     }
  234.  
  235.    
  236.  
  237.     private void createNote() {
  238.  
  239.         Intent i = new Intent(this, NoteEdit.class);
  240.  
  241.         startActivityForResult(i, ACTIVITY_CREATE);
  242.  
  243.     }
  244.  
  245.    
  246.  
  247.     @Override
  248.  
  249.     protected void onListItemClick(ListView l, View v, int position, long id) {
  250.  
  251.         super.onListItemClick(l, v, position, id);
  252.  
  253.         Intent i = new Intent(this, NoteEdit.class);
  254.  
  255.         i.putExtra(NotesDbAdapter.KEY_ROWID, id);
  256.  
  257.         startActivityForResult(i, ACTIVITY_EDIT);
  258.  
  259.     }
  260.  
  261.  
  262.  
  263.     @Override
  264.  
  265.     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  266.  
  267.         super.onActivityResult(requestCode, resultCode, intent);
  268.  
  269.         fillData();
  270.  
  271.     }
  272.  
  273.    
  274.  
  275.     @Override
  276.  
  277.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo info) {
  278.  
  279.                 Log.v(TAG, "Created context menu.");
  280.  
  281.                 menu.setHeaderTitle(R.string.context_title);
  282.  
  283.                 MenuItem mItem = menu.add(Menu.NONE, CONTEXT_SHARE_ID, Menu.NONE, "TESTING");
  284.  
  285.                 Log.v(TAG, "Added menu item: "+mItem.getItemId());
  286.  
  287.                 mItem = menu.add(Menu.NONE, CONTEXT_DELETE_ID, Menu.NONE, R.string.menu_delete);
  288.  
  289.                 Log.v(TAG, "Added menu item: "+mItem.getItemId());
  290.  
  291.         }
  292.  
  293. }
  294.  
  295.  
Parsed in 0.050 seconds, using GeSHi 1.0.8.4
snappy
Freshman
Freshman
 
Posts: 5
Joined: Thu Dec 11, 2008 9:29 pm

Top

Postby snappy » Fri Dec 12, 2008 7:13 am

Nevermind this is now solved (for me anyways). All the documentation on code.google.com didn't help. Infact a lot of it is quite useless.

I had to get the source and rape & paste from the email application, particularly the file: Email/src/com/android/email/activity/Accounts.java shows how it's done (using XML!).
snappy
Freshman
Freshman
 
Posts: 5
Joined: Thu Dec 11, 2008 9:29 pm

Postby mccarthycw » Fri Jul 17, 2009 5:48 pm

Snappy, thanks for posting the solution you found.. I have a similar problem but its my ContextMenu never gets created. I will have to check out the above mentioned file.
mccarthycw
Freshman
Freshman
 
Posts: 4
Joined: Fri Jul 17, 2009 5:40 pm
Location: Mechanicsburg, Pennsylvania, USA

The cause...

Postby RNekic » Thu Nov 19, 2009 5:41 pm

Hi all, I just spent quite a bit of time pulling my hair out over this and finally discovered the cause and solution.

If the context menu is displaying but onContextItemSelected does not fire when you select a context menu item, it's likely because you've overridden onMenuItemSelected. Make sure your code in onMenuItemSelected calls super.onMenuItemSelected and your context menu will start behaving properly.
RNekic
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 19, 2009 4:45 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 15 guests