Detecting which selected item (in a ListView) spawned the ContextMenu (Android)
I do exactly this. In my onCreateContextMenu(...)
method, I cast the ContextMenu.ContextMenuInfo
to AdapterView.AdapterContextMenuInfo
. From there, you can get the targetView, which you cast again to the widget. The complete code is available in HomeActivity.java, look for the onCreateContextMenu(...)
method.
@Overridepublic void onCreateContextMenu(ContextMenu contextMenu, View v, ContextMenu.ContextMenuInfo menuInfo) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; selectedWord = ((TextView) info.targetView).getText().toString(); selectedWordId = info.id; contextMenu.setHeaderTitle(selectedWord); contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit); contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);}
Note that I store the selected text as well as the select id in private fields. Since the UI is thread confined, I know the selectedWord and selectedWordId fields will be correct for later actions.
First of all, I'm wondering if you're making things a little overly complicated by using View.setOnCreateContextMenuListener()
. Things get a lot easier if you use Activity.registerForContextMenu()
, because then you can just use Activity.onCreateContextMenu()
and Activity.onContextItemSelected()
to handle all of your menu events. It basically means you don't have to define all these anonymous inner classes to handle every event; you just need to override a few Activity methods to handle these context menu events.
Second, there's definitely easier ways to retrieve the currently selected item. All you need to do is keep a reference either to the ListView
or to the Adapter
used to populate it. You can use the ContextMenuInfo as an AdapterContextMenuInfo to get the position of the item; and then you can either use ListView.getItemAtPosition()
or Adapter.getItem()
to retrieve the Object
specifically linked to what was clicked. For example, supposing I'm using Activity.onCreateContextMenu()
, I could do this:
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Get the info on which item was selected AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; // Get the Adapter behind your ListView (this assumes you're using // a ListActivity; if you're not, you'll have to store the Adapter yourself // in some way that can be accessed here.) Adapter adapter = getListAdapter(); // Retrieve the item that was clicked on Object item = adapter.getItem(info.position);}@Overridepublic boolean onContextItemSelected(MenuItem item) { // Here's how you can get the correct item in onContextItemSelected() AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); Object item = getListAdapter().getItem(info.position);}
this is another way on how to create context menu n how to delete the item selected here is the whole code
public class SimpleJokeList extends Activity {public static final int Upload = Menu.FIRST + 1;public static final int Delete = Menu.FIRST + 2;int position;ListView lv;EditText jokeBox;Button addJoke;MyAdapter adapter;private ArrayAdapter<String> mAdapter;private ArrayList<String> mStrings = new ArrayList<String>();String jokesToBeAdded;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simplejokeui); lv=(ListView)findViewById(R.id.jokelist); addJoke=(Button)findViewById(R.id.addjoke); jokeBox=(EditText)findViewById(R.id.jokebox); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings); registerForContextMenu(lv); listItemClicked(); addJokes();private void addJokes() { // TODO Auto-generated method stub addJoke.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub jokesToBeAdded=jokeBox.getText().toString(); if(jokesToBeAdded.equals("")){ Toast.makeText(getApplicationContext(), "please enter some joke", Toast.LENGTH_LONG).show(); } else{ lv.setAdapter(mAdapter); mAdapter.add(jokesToBeAdded); jokeBox.setText(null); } } });}private void listItemClicked() { // TODO Auto-generated method stub lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub position=arg2; return false; } });}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); populateMenu(menu); menu.setHeaderTitle("Select what you wanna do");}private void populateMenu(ContextMenu menu) { // TODO Auto-generated method stub menu.add(Menu.NONE, Upload, Menu.NONE, "UPLOAD"); menu.add(Menu.NONE, Delete, Menu.NONE, "DELETE");} @Override public boolean onContextItemSelected(MenuItem item) { return (applyMenuChoice(item) || super.onContextItemSelected(item)); }private boolean applyMenuChoice(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case Delete: String s=mAdapter.getItem(position); mAdapter.remove(s); // position--; Toast.makeText(getApplicationContext(),"Congrats u HAve Deleted IT", Toast.LENGTH_LONG).show(); return (true); } return false;}