I have a class that extends SherlockActivity because I'm using an ActionBarSherlock action bar in the activity. However, I'm also trying to add a context menu to a listview in this activity that launches when an item in the listview is longpressed. I'm getting an error in my declaration of onContextItemSelected(MenuItem item)
that says that my override is invalid because there's nothing to override. However, when I remove the override, the function is never called. I know this has something to do with the way SherlockActivity inherits from Activity but I'm not sure about the specifics. Maybe I'm missing an import? See the relevant code here:
public class Inbox extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inbox);
refreshMsgs(this.getCurrentFocus());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lstInbox){
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String contactName = lstConversation.get(info.position).getContactName();
menu.setHeaderTitle(contactName);
menu.add(Menu.NONE, 0, 0, "View contact");
menu.add(Menu.NONE, 1, 1, "Delete thread");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
SmsConversation selectedConvo = lstConversation.get(info.position);
if (item.getItemId() == 0){
//view contact
Intent viewcontactIntent = new Intent();
String contactId = selectedConvo.getContactId();
Uri uriContact = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
viewcontactIntent.setData(uriContact);
PendingIntent pViewIntent = PendingIntent.getActivity(getBaseContext(), 1, viewcontactIntent, 0);
try {
pViewIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
} else {
//DELETE thread
}
return true;
}
}
And here are all of my imports for the class:
import java.util.ArrayList;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Html;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
Faced with a similar issue a long time ago, I have overriden onContextItemSelected()
by forcing the normal MenuItem
using its fully qualified class name (android.view.MenuItem
), like so:
@Override
public boolean onContextItemSelected (android.view.MenuItem item)
{
//implementation
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With