Question

I added a context menu to a list .But the list shows context menu even if there is no list item. Here is my code

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.add(getString(R.string.Delete));
    }
}
/* (non-Javadoc)
 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
 */
@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    int menuItemIndex = item.getItemId();   
    showDeleteDialog(getString(R.string.deletemsg));
    return super.onContextItemSelected(item);

}

How can i solve this problem?

No correct solution

OTHER TIPS

In the onCreate add a check before building the menu.

Assuming your list items adapter object is called listItemsAdapter

  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;



    if(info != null)
    {
    if (v.getId()==R.id.list) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            menu.add(getString(R.string.Delete));
        }
    }

Is it when you click on an empty cell or a cell which has a value. If it is only an empty cell then I have modified the code accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top