Passing cursor/DB values through the Context Menu

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

Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 4:56 am

My code uses the cursor adapter to populate the ListView from the values that I get from the SQLite db. However, I don't know how am I supposed to get the id ( "_id" ) - or any other field values for that matter - when I call the onCreateContextMenu?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Top

Re: Passing cursor/DB values through the Context Menu

Postby padde » Mon Jun 28, 2010 5:46 am

Why onCreateContextMenu? If you want to react on longclicks inside a listview you should go for an
onItemLongClickListener.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
  2.     public boolean onItemLongClick(AdapterView<?> a0, View a1, int a2, long a3) {
  3.         // a3 is your _id
  4.         // a2 is the position in the list
  5.     }
  6. });
  7.  
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


If you stuck on the onCreateContextMenu for any reason take a look into the ContextMenuInfo object
that you get. Documentation of ContextMenuInfo says:
"Additional information regarding the creation of the context menu. For example, AdapterViews use
this to pass the exact item position within the adapter that initiated the context menu."
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 10:01 am

padde wrote:Why onCreateContextMenu? If you want to react on longclicks inside a listview you should go for an
onItemLongClickListener.


OK, but how do I create the menu then?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby asolanki » Mon Jun 28, 2010 1:23 pm

Hi!!

you can register context menu for any view by using this method

registerForContextMenu(View v)

then you need to override two functions

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  @Override
  2.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  3.         menu.setHeaderTitle("Sample menu");
  4.         menu.add(0, 0, 0, R.string.expandable_list_sample_action);
  5.     }
  6.    
  7.     @Override
  8.     public boolean onContextItemSelected(MenuItem item) {
  9.        
  10.        
  11.         return false;
  12.     }
  13.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4


hope this will help you
asolanki
Senior Developer
Senior Developer
 
Posts: 151
Joined: Thu Jan 08, 2009 7:32 am

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 6:46 pm

asolanki wrote:Hi!!

you can register context menu for any view by using this method

registerForContextMenu(View v)

then you need to override two functions

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  @Override
  2.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  3.         menu.setHeaderTitle("Sample menu");
  4.         menu.add(0, 0, 0, R.string.expandable_list_sample_action);
  5.     }
  6.    
  7.     @Override
  8.     public boolean onContextItemSelected(MenuItem item) {
  9.        
  10.        
  11.         return false;
  12.     }
  13.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


hope this will help you


I understand this, however, my list is generated from the database and I'm using the CursorAdapter. I need to pass the data ( like the "_id" column value ) of the item list that was selected to that context menu. How do I do that?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby anarche » Mon Jun 28, 2010 10:14 pm

Include select ("_id", ...) in your select statement
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 10:18 pm

anarche wrote:Include select ("_id", ...) in your select statement


I have. But can you tell me how am I supposed to get that field's value into the onCreateContextMenu?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby padde » Mon Jun 28, 2010 10:42 pm

The onCreateContextMenu function provides you with an ContextMenuInfo object.. from this object you can
get the ID... you dont need to pass it manually .. you get it passed from the OS.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 10:56 pm

padde wrote:The onCreateContextMenu function provides you with an ContextMenuInfo object.. from this object you can
get the ID... you dont need to pass it manually .. you get it passed from the OS.


That ID is the id of the item in the list but not the "_id" in the database. Isn't it?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby padde » Mon Jun 28, 2010 11:33 pm

Should the be the _id.. if its not its the position in the list and then you can use the
"getItemIdAtPosition" method of your ListView.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Mon Jun 28, 2010 11:43 pm

padde wrote:Should the be the _id.. if its not its the position in the list and then you can use the
"getItemIdAtPosition" method of your ListView.


Can you tell me how would that line be coded?
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Tue Jun 29, 2010 12:09 am

BTW, this is what I have so far per what you've adviced:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreateContextMenu( ContextMenu menu, View v,  ContextMenu.ContextMenuInfo menuInfo)  {
  2.                
  3.                 super.onCreateContextMenu(menu, v, menuInfo);
  4.                
  5.                 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
  6.                 Cursor cursor = ((Cursor) getListView().getItemAtPosition(info.position));
  7.                 String test = cursor.getString(cursor.getColumnIndex("_id"));
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


However, the "test" string comes back as "0" for all items.
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby anarche » Tue Jun 29, 2010 12:19 am

id is an integer. use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. cursor.getInt(cursor.getColumnIndex("_id"))
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


How many rows are being returned? If only 1, then 0 is the expected result.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Tue Jun 29, 2010 12:35 am

There are about a dozen of rows. This is the code for my SQL call and the adapter:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final Cursor c = myDB.rawQuery("SELECT _id, priority, task, tcreated" +
  2.                         " FROM " + MY_DATABASE_TABLE
  3.                         + " order by priority desc, tcreated;",
  4.                         null);         
  5.  
  6. ToDoListAdapter adaptor = new ToDoListAdapter(this,c);
  7. setListAdapter(adaptor);
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Re: Passing cursor/DB values through the Context Menu

Postby kidalex » Tue Jun 29, 2010 1:14 am

I figured it out!!! My table wasn't created so that the _id field was actually populated with my INSERT command. Now it does and my code works! Thank you so much for everybody who helped! Hope to see you tomorrow at SF's meetup.com gig
kidalex
Developer
Developer
 
Posts: 42
Joined: Tue Jun 22, 2010 10:03 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 25 guests