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 7700363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:41:28+00:00 2026-05-31T22:41:28+00:00

I need to access the text in a ListView item through a long click

  • 0

I need to access the text in a ListView item through a long click selection. For old Android versions I have successfully done this with context menus with the code below.

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    String text = ((TextView) info.targetView).getText().toString();

    switch (item.getItemId()) {
        case R.id.getText:
            getText(text);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

For newer Android versions, however, I would like to do this with the Contextual Action Bar but can’t figure out how to extract the selected text after having selected an item in the bar. The below code does not work.

    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    myListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        String text = ((TextView) info.targetView).getText().toString();

            switch (item.getItemId()) {
                case R.id.contextDelete:
                    getText(text);
                    return true;;
                default:
                    return false;
            }
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }

        //Other actionmode methods...
    });

The Contextual Action Bar shows up ok, but when picking an item from it I get a NullPointerException at the AdapterContextMenuInfo line, since this was obviously made for context menus and not action bars. Is there some equivalent for this for Action bars perhaps? Or how can I get the ListView item text in this case? Thanks.

  • 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-05-31T22:41:29+00:00Added an answer on May 31, 2026 at 10:41 pm

    getCheckedItemPositions() on ListView will return the item positions the user has checked, and getCheckedItemIds() will return their ID values if you are using something like CursorAdapter.

    Here is a sample project demonstrating the use of CHOICE_MODE_MULTIPLE_MODAL on API Level 11+ and falling back to context menus on older devices.

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

Sidebar

Related Questions

There are some text files(Records) which i need to access using C#.Net. But the
I need to create an ANSI text file from an Access recordset that outputs
I have two different modules that need access to a single file (One will
I need to access clipboard in silverlight. in winform apllication: textBox1.Text = Clipboard.GetText(TextDataFormat.Html); but,
I am working on MVC 4.0. I need to access my dropdown box Text
I have 8 NSTextFields and I need to access them by UI to determine
I have a document which has a nested frameset. I need to access one
I need to access myfile.txt file using FileReader in Android , please suggest me
I need to access child window elements from parent window. I have written the
I have a problem with Custom cell on story board. I need to access

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.