gpt4 book ai didi

android - 如何使用 onItemLongClick 显示上下文菜单?

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

我试图在这里得到答案,但没有得到任何完美的答案。我试图在 onItemLongClick 上显示上下文菜单,但没有成功,因为我同时使用了 onItemLickonItemLongClick

我正在使用 onItemClick 开始一个新 Activity ,但两者都没有成功。

这是代码

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_contacts);

contactList = new ArrayList<HashMap<String,String>>();

new LoadAllContacts().execute();

registerForContextMenu(getListView());

ListView listView = getListView();

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int postion, long id) {
registerForContextMenu( view );
openContextMenu( view );
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
Intent intent = new Intent(AllContactsActivity.this, editContactActivity.class);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(resultCode == 100)
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listview_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.editContactMI:
Intent i = new Intent(getApplicationContext(), editContactActivity.class);
i.putExtra(TAG_ID, cId);
i.putExtra(TAG_NAME, cName);
i.putExtra(TAG_CONTACT_NO, cNumber);
startActivityForResult(i, 100);
cId = null;
cName = null;
cNumber = null;
break;
case R.id.deleteContactMI :
new DeleteContact().execute();
break;
case R.id.saveContactMI:
break;
default:
cId = null;
cName = null;
cNumber = null;
break;
}

return true;
}

最佳答案

I am trying to show context menu on onItemLongClick

要使用上下文菜单系统,您无需实现 OnItemLongClickListener。相反,您调用 registerForContextMenu()(例如,从 Activity 的 onCreate())。只需从上面显示的代码中删除 OnItemLongClickListener,您的运气就会好一些。

关于android - 如何使用 onItemLongClick 显示上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924414/

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