Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContextMenu not popping up on Long click

The context menu is not popping up on the long click on the list items in the list view. I've extended the base adapter and used a view holder to implement the custom list with textviews and an imagebutton.

adapter = new MyClickableListAdapter(this, R.layout.timeline, mObjectList);
       list.setAdapter(adapter);
       registerForContextMenu(list);  

Implementation of onCreateContextMenu

  @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  // TODO Auto-generated method stub
  super.onCreateContextMenu(menu, v, menuInfo);

  Log.d(TAG, "Entering Context Menu");

   menu.setHeaderTitle("Context Menu");

  menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
  .setIcon(R.drawable.icon);
 }

The XML for listview is here

 <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

I've been trying this for many days. I think its impossible to register Context-menu for a custom list view like this. Correct me if I am wrong (possibly with sample code).

Now I am thinking of a adding a button to the list item and it displays a menu on clicking it. Is it possible with some other way than using Dialogs?

Any help would be much appreciated..

like image 723
Primal Pappachan Avatar asked May 18 '10 05:05

Primal Pappachan


2 Answers

Such problem arises in list view when it has focusable items like checkbox, radioButton,etc. To solve this in the layout for the list item for the focusable items include :

android:focusable="false";
like image 127
Vicky Kapadia Avatar answered Oct 24 '22 23:10

Vicky Kapadia


Why didn't you use ListActivity?

In my ListActivity I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* setContentView() and all stuff that happens in this method */
    registerForContextMenu(getListView());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
    return;
    }

    Something something = (Subway) getListAdapter().getItem(info.position);
    menu.setHeaderTitle(something.getName());
    menu.setHeaderIcon(something.getIcon());
    menu.add(0, CONTEXT_MENU_SHARE, 0, "Do something!");
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return false;
    }

    switch (item.getItemId()) {
        case DO_SOMETHING:
            /* Do sothing with the id */
            Something something = getListAdapter().getItem(info.position);
            return true;
    }
like image 39
Macarse Avatar answered Oct 24 '22 23:10

Macarse