Lior Iluz
Lior Iluz

Reputation: 26563

ContextMenu title -> get from clicked listView item

Trying to set ContextMenu title according to the ListView clicked item. The ListView contains Bookmarks list -> FAVICON + BOOKMARK TITLE

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      menu.add(0, EDIT_ID, 0, R.string.menu_edit);
      menu.add(0, DELETE_ID, 0, R.string.menu_delete);
      menu.add(0, SHARE_ID, 0, R.string.menu_share);

      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
      View itemID = (info.targetView);
      menu.setHeaderTitle("bla" + itemID);
    }

When I run this code it shows the android.widget.RelativeLayout@423d2389 or whatever and if I change the itemID to String itemID = ((TextView) info.targetView).getText().toString(); I get force close on long click even though no errors are shown in Eclipse or when I run the app.

I also want to get the favicon in the same way...

Appreciate the help!

Upvotes: 3

Views: 3185

Answers (1)

broot
broot

Reputation: 28332

info.targetView is a RelativeLayout, not a TextView. Add android:id attribute to your title TextView (e.g. android:id="@android:id/title") and then get string value by:

String title = ((TextView) info.targetView.findViewById(android.R.id.title)).getText();

Upvotes: 5

Related Questions