onContextItemSelected Not Called When Including ABS

1,401 views
Skip to first unread message

Rob Isakson

unread,
Jan 11, 2012, 11:02:58 AM1/11/12
to actionba...@googlegroups.com
So I've experienced this in 2 separate applications. After adding adding ActionBarSherlock to the project and adding an action bar to the activity, the onContextItemSelected never gets fired when an item is clicked from a long press context menu. I've tried a bunch of different things from my various googlings, but nothing seems to work. Any thoughts?

ListView list = (ListView) findViewById(R.id.scroll_list);
list.setAdapter(this.custom_adapter);
registerForContextMenu(list);
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0, CONTEXT_EDIT, 0, "Edit");
menu.add(0, CONTEXT_DELETE, 1, "Delete");
}
}); 

public boolean onContextItemSelected(MenuItem item) {
Log.d("debug", "onContextItemSelected-item.getItemId():" + item.getItemId());
}

Jake Wharton

unread,
Jan 11, 2012, 11:12:49 AM1/11/12
to actionba...@googlegroups.com
You callback should explicitly be using android.view.MenuItem for context menu callbacks and not android.support.v4.view.MenuItem (which I'm going to assume is imported for normal options menu usage). If you add an @Override annotation to the callback you can ensure you're using the right type.

---
Jake Wharton
http://about.me/jakewharton

Rob Isakson

unread,
Jan 11, 2012, 11:25:41 AM1/11/12
to actionba...@googlegroups.com
Ahh that makes sense, but now onOptionsItemSelected is erroring, stating "Cannot override the final method from FragmentActivity", even though I am not @Override'ing that method.

Thanks,
Rob Isakson

PogramarProgramerProgramor. I write code.

Jake Wharton

unread,
Jan 11, 2012, 11:29:24 AM1/11/12
to actionba...@googlegroups.com
You'll have to specify the full package for one of them.

e.g.,

import android.support.v4.view.MenuItem

class Whatever extends FragmentActivity {
    public boolean onOptionsItemSelected(MenuItem item) {
        /* ... */
    }

    public boolean onConextItemSelected(android.view.MenuItem item) {
        /* ... */
    }

Rob Isakson

unread,
Jan 11, 2012, 11:32:17 AM1/11/12
to actionba...@googlegroups.com
That did it! Thanks Jake!


Thanks,
Rob Isakson

PogramarProgramerProgramor. I write code.




On Wed, Jan 11, 2012 at 10:29 AM, Jake Wharton <jakew...@gmail.com> wrote:
android.support.v4.view.

Reply all
Reply to author
Forward
0 new messages