user3558118
user3558118

Reputation: 153

Getting OnLongClick to work in ListView Android Studio

I am currently trying to get onLongClick on each item of my listview but android studio will not recognise super.onCreateContextMenu(menu, v, menuInfo); and super.onContextItemSelected(item);. When I take these out noting works at all.

Inside onCreate I have

registerForContextMenu(getListView());

Then below this function I have the following functions

 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                         super.onCreateContextMenu(menu, v, menuInfo);
                         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                         menu.setHeaderTitle(mAdapter.getItem(info.position).toString());
                         menu.add(Menu.NONE, CONTEXT_MENU_DELETE, CONTEXT_MENU_DELETE, R.string.del_item);
                     }



             public boolean onContextItemSelected(MenuItem item) {
                        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                        switch (item.getItemId()) {
                            case CONTEXT_MENU_DELETE:
                                mAdapter.remove(info.position);
                                return true;
                            default:
                            return super.onContextItemSelected(item);
                        }
                        return true;
                    }

Upvotes: 0

Views: 331

Answers (1)

Piyush
Piyush

Reputation: 1973

Make sure the imports are from the compatibility library and not from the SDK itself.

Upvotes: 2

Related Questions