开发者

ContextMenu not popping up on Long click

开发者 https://www.devze.com 2022-12-31 15:44 出处:网络
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 image

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..


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";


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;
    }


Use OnItemLongClickListener (via the set~) method of the ListView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号