在ActionBarSherlock中覆盖onContextItemSelected()

时间:2013-01-24 01:49:23

标签: android actionbarsherlock

我有一个扩展SherlockActivity的类,因为我在活动中使用了ActionBarSherlock操作栏。但是,我还尝试将上下文菜单添加到此活动中的列表视图中,该列表视图在列表视图中的项目被长按时启动。我在onContextItemSelected(MenuItem item)声明中收到错误,指出我的覆盖无效,因为没有任何内容可以覆盖。但是,当我删除覆盖时,永远不会调用该函数。我知道这与SherlockActivity继承Activity的方式有关,但我不确定具体细节。也许我错过了一个导入?请参阅此处的相关代码:

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;
}
}

以下是我对该课程的所有导入:

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;

1 个答案:

答案 0 :(得分:23)

很久以前面对类似的问题,我通过使用其完全限定的类名(onContextItemSelected())强制普通MenuItem来覆盖android.view.MenuItem,如下所示:

@Override
public boolean onContextItemSelected (android.view.MenuItem item)
{
  //implementation
}