AndroidDev
AndroidDev

Reputation: 16385

Android ListView ContextMenu not appearing

I have a CustomListAdapter. I have overloaded my OnItemClickListener and added a registerForContextMenu line for the position that i have a context menu shown.

When i select the Item that should show the MenuItem, the menu is shown. When i press the back button on the phone the menu disappears. However now what happens that the same Item in my listview does not receive the OnItemClickListener anymore. Am i making sense ? I mean after the menu disappears, the same item does not receive the click listener. The items above and below receive the event as desired. I seems as if the Menu has disappeared but still is catching the click event ?

Upvotes: 0

Views: 1157

Answers (2)

AndroidDev
AndroidDev

Reputation: 16385

        quickLinkListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id)
            {

                Intent intent = new Intent();
                intent.setClassName(Home.this, "com.myapp.toc" + Constants.ACT_NAMES[position]);

                if (position < 4 && position > 1)
                {

                    switch (position)
                    {

                    case 3:
                        registerForContextMenu(v);
                        ViewHolder.v=v;
                        openContextMenu(v);
                        break;
                    }
                }

            }
        });

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_home, menu);
    menu.setHeaderTitle("Select Video Type");
}

Upvotes: 0

Simon Dorociak
Simon Dorociak

Reputation: 33505

It's bad. You have to call registerForContextMenu in onCreate method.

So try it like this:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.contacts);
   registerForContextMenu(<yourListView>);
   ...
}

for create ContextMenu you have to override onCreateContextMenu method

@Override
public void onCreateContextMenu(ContextMenu cMenu, View parent, ContextMenu.ContextMenuInfo info) {
   this.contextMenu = cMenu;
   new MenuInflater(Contacts.this).inflate(R.menu.conmenu, this.contextMenu);
}

and for select items override onContextItemSelected method:

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)       item.getMenuInfo();
   switch (item.getItemId()) {
      case R.id.c_odobrat:
         deleteContactDialog(info.id);
         return true;
   }
   return false;
}

And it should works.

Upvotes: 1

Related Questions