Getting a single item from listfragment using ContextMenu

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

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

  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(;
        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 version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
android:layout_height="match_parent" >

    android:text="Large Text"
    android:textSize="30dp" />


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

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

        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(;
            n = tv.getText().toString();
            edit.putString(SmsPrefs.NAME, n).commit();
            return false;

but that method does not seem to work here


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.

Need Your Help

In Log Shipping configuration with SQL2005, Primary server's backup job not being added

sql-server-2005 failover

I'm creating a log shipping configuration between two sql server 2005 instances. I'm shipping the logs of a single database and I'm not using a Monitor service (for the time being). When I run th...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.