Chirag
Chirag

Reputation: 56925

Show Selected item on context menu header

I have created an application for Android that has an activity that has a ListView which lists all playlists.

I have added a ContextMenu, so that a long-click brings up several options such as "Delete", "Play", etc.

In the ContextMenu handler, how can I extract information to know which playlist the context menu has been clicked for.

Here I want to display the selected playlist name on context menu header.

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

     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Selected Playlist Name");      
     menu.add(0, v.getId(), 0, "Play");     
     menu.add(0, v.getId(), 1, "Delete"); 
}

Upvotes: 0

Views: 4120

Answers (1)

iamtheexp01
iamtheexp01

Reputation: 3466

This is how to get the text in the selected item in the ListView. You can read the information from the AdapterContextMenuInfo when you cast menuInfo to the type AdapterContextMenuInfo and then read the value from the property targetView)

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
   super.onCreateContextMenu(menu, v, menuInfo);
   AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
   String name = ((TextView) info.targetView).getText().toString();
   menu.setHeaderTitle(name);      
   menu.add(0,v.getId(), 0, "Play");    
   menu.add(0,v.getId(), 1, "Delete"); 
}

Upvotes: 2

Related Questions