gpt4 book ai didi

android - 在 ActionBarSherlock 中覆盖 onContextItemSelected()

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:50 25 4
gpt4 key购买 nike

我有一个扩展 SherlockActivity 的类,因为我在 Activity 中使用了 ActionBarSherlock 操作栏。但是,我还尝试将上下文菜单添加到此 Activity 中的 ListView ,该 Activity 在长按 ListView 中的项目时启动。我在 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;
}
}

这是我为该类(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;

最佳答案

很久以前遇到过类似的问题,我通过强制普通 MenuItem 使用其完全限定的类名 (android. view.MenuItem),像这样:

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

关于android - 在 ActionBarSherlock 中覆盖 onContextItemSelected(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14492612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com