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

  • SEARCH
  • Home
  • 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 8737387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:30:35+00:00 2026-06-13T10:30:35+00:00

I need the ID from an item in onContextItemSelected() . I use the following

  • 0

I need the ID from an item in onContextItemSelected(). I use the following code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.listView1) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle("Optionen");
        menu.add(Menu.NONE, info.position, 0, "Artikel entfernen");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {       
    final ListView lv = (ListView)findViewById(R.id.listView1);
    Toast.makeText(getApplicationContext(), lv.getItemAtPosition(item.getItemId()).toString(), Toast.LENGTH_LONG).show();
    return true;
}

The info.position var could not be read from onContextItemSelected with item.getItemId! The app crashed with the log: String empty. Can you find a mistake? Thanks!

UPDATE
I changed the code to:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.listView1) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle("Optionen");
    menu.add(Menu.NONE, info.position, 0, "delete post");
  }
}

@Override
public boolean onContextItemSelected(MenuItem item) {

  final ListView lv = (ListView)findViewById(R.id.listView1);
  //lv.getItemAtPosition(0).toString()


  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();


  Toast.makeText(getApplicationContext(), info.position, Toast.LENGTH_LONG).show();
  //Toast.makeText(getApplicationContext(), lv.getItemAtPosition(item.getItemId()).toString(), Toast.LENGTH_LONG).show();


  return true;
}

But also this code doesn’t work (NotFoundException). I have a listview (R.id.listView1) with items. If the user clicks on an item long, the context menu appears. There is the option “delete post”. Then I need the ID from the post to delete it!

Are there other solutions?

UPDATE 2
I solved the problem:

Toast.makeText(getApplicationContext(), lv.getItemAtPosition(info.position).toString(), Toast.LENGTH_LONG).show();
  • 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-13T10:30:36+00:00Added an answer on June 13, 2026 at 10:30 am

    info.id will be the id value associated with the given item in the AdapterView.

    To get at info in onContextItemSelected(), use:

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    

    Also, note that context menus are becoming much less popular, in favor of action modes (a.k.a., contextual action bars) with an action bar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
Possible Duplicate: Get random item from array I need a code to set a
I need help to get a response when I click on an Item from
I need recommendations from people whom have implemented an easy to use GWT editable
I need to select an item from a list box that contains objects of
I need to override the back button to launch a context menu. I have
I need from one JSP open another JSP, and send POST parameters to this
What I need from the string ((VBD)(((JJ))(CC)((RB)(JJ)))((IN)((DT)(JJ)(NNP)(NNPS)))) is this: JJ, RBJJ, DTJJNNPNNPS, JJCCRBJJ, INDTJJNNPNNPS
What are the necessary items need from creating a deployment project for a windows
I am new to SVN. Need help from you guys. I have a link

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.