Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:48:14+00:00 2026-06-17T17:48:14+00:00

I got a application with a listView in which I setup data from my

  • 0

I got a application with a listView in which I setup data from my sqlite database. My CustomCursorAdapter is placing the data (from the DBAdapter) in the listview.
I want to get the item id of the selected item from my listView, I got it working via this code:

listView.setOnItemClickListener(new OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> listView, View view, 
             int position, long id) {
           // Get the cursor, positioned to the corresponding row in the result set
           Cursor cursor = (Cursor) listView.getItemAtPosition(position);

           //Get it from the database.
           String countryCode = 
           cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0)));
           Toast.makeText(getApplicationContext(),
             countryCode, Toast.LENGTH_SHORT).show();

           }
          });

That code does get the ID and makes a toast in which I can see the ID. This works perfectly.
Now I want to put it in a context menu in which I long click the item (so this method does not start). I have the context menu working with the items, but I only need to get the id of which item in the listview is selected.

This is now my code for the context menu:

     public boolean onContextItemSelected(MenuItem item) {

     Cursor cursor = (Cursor) listView.getItemAtPosition(position);


       String countryCode = 
       cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0)));


      switch (item.getItemId()) {
              case CONTEXT_MENU_DELETE_ITEM:

               Toast.makeText(getApplicationContext(),
                     countryCode, Toast.LENGTH_SHORT).show();
                   return(true);
             case CONTEXT_MENU_UPDATE:

                   return(true);    
      }
  return(super.onOptionsItemSelected(item));
}

The context menu does not work now because the position variabele is not set. I really dont know how to get the selected item.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:48:15+00:00Added an answer on June 17, 2026 at 5:48 pm

    Use this

    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo();   
    int positionSelected = menuInfo.position; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an application that downloads data from a 3rd party at 3am every
I've got an application, which uses many listBoxes with data attached to them using
I'm developing a small application that reads data from a database and displys it
I got a custom ListView which I fill with an Adaptar. During my application
How to get the application icon that i got from the result into arraylist?
I've got an application with a ListView and I want to loop through each
I am currently developing a C# application which has got it's own logging. When
I've got an application which displays a field that can have multiple entries but
I've got a wpf application which shows a list of users. clicking on a
I've got a gridview in my wpf application, the xaml of which looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.