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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:42:53+00:00 2026-05-27T05:42:53+00:00

I am trying to get a single item from a listfragment when long pressed

  • 0

I am trying to get a single item from a listfragment when long pressed is detected using context menu. the thing is the item it gets is the first item in the list and not the one selected, meaning…

if my list looked like this

item 5
item 4
item 3
item 2
item 1

and I long press on say item 2

it will give me item 5

@Override
  public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
      super.onCreateContextMenu(menu, v, menuInfo);
      menu.add(Menu.NONE,Menu.NONE,Menu.NONE,"Delete");
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {

        Vibrator vibrator = (Vibrator)getActivity().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long [] {0,20,0,0}, -1);
        TextView tv = (TextView)getActivity().findViewById(R.id.bListTextView);
        name = tv.getText().toString();
        Log.d("List", name);
        Cursor cID = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,new String[] {BowlersDB.ID,BowlersDB.NAME},BowlersDB.NAME + "=" + name,null,null);
        String nameID = cID.getString(1);
        getActivity().getContentResolver().delete(BowlersDB.CONTENT_URI,"_id" + "=" + nameID,null);
      return super.onContextItemSelected(item);
  }

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/bListTextView"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30dp" />

</RelativeLayout>

in a list view I normally just get the textview by id like this

mContactList = (ListView) findViewById(R.id.contactList);
mContactList.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {

            Vibrator v = (Vibrator)getSystemService(VIBRATOR_SERVICE);
            v.vibrate(new long [] {0,20,0,0}, -1);
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Contacts.this);
            SharedPreferences.Editor edit = pref.edit();
            TextView tv =(TextView)arg1.findViewById(R.id.contactEntryText);
            n = tv.getText().toString();
            edit.putString(SmsPrefs.NAME, n).commit();
            showDialog(1);
            return false;
        }
    });

but that method does not seem to work here

  • 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-27T05:42:54+00:00Added an answer on May 27, 2026 at 5:42 am

    In your onContextItemSelected you first need to fetch the selected item:

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

    Then you can decide whether to query through id or position, you can get both from AdapterContextMenuInfo.

    The way you’re doing it (with findViewById) I suppose it will just fetch the first view that it can find in the view hierarchy, i.e. the first in the list.

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

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I'm trying to grab a single item from each of the Lists here, and
I am trying to get some data from a GoogleCheckout response. I am using
I'm trying to get the offset or position of the cursor on a single
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I'm trying to remove an item from an array based on string; public function
I'm trying to get a list of JSON objects (products) from a local file
I am trying to display and capture only the most recent item from a
I'm trying to figure out how to calculate difference from previous item, when the

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.