Android:如何找到从上下文菜单中单击的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2453620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to find the position clicked from the context menu
提问by Sosi
I have a list view filled with data. I set up a context menu for the listview using the following code:
我有一个充满数据的列表视图。我使用以下代码为列表视图设置了上下文菜单:
list.setOnCreateContextMenuListener
(
new View.OnCreateContextMenuListener()
{
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
AdapterContextMenuInfo mi =(AdapterContextMenuInfo) menuInfo;
menu.add(0, 0, 0, "Delete item");
}
}
);
I have the following method override to control de contextmenu menuitem selected:
我有以下方法覆盖来控制选择的上下文菜单菜单项:
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 0:
ShowAlert("hello from delete item");
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
In this overrided method, how could i find the item of the list view that was clicked?
在这个被覆盖的方法中,我怎么能找到被点击的列表视图的项目?
Thanks in advance. Best Regards. Jose
提前致谢。此致。何塞
回答by Dimitar Dimitrov
You can use the ContextMenu.ContextMenuInfo
.
您可以使用ContextMenu.ContextMenuInfo
.
Something like that:
类似的东西:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
}
You can also get the exact View
for which the menu is being displayed:
您还可以获得View
正在显示的菜单的确切信息:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
View view = info.targetView;
}
回答by Pentium10
private static final int EDIT_ID = Menu.FIRST + 3;
private static final int DELETE_ID = Menu.FIRST + 4;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit").setAlphabeticShortcut(
'e');
menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
.setAlphabeticShortcut('d');
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case EDIT_ID:
edit(info.id);
return (true);
case DELETE_ID:
delete(info.id);
return (true);
}
return (super.onOptionsItemSelected(item));
}
回答by user6757534
OK, to solve problem info null **** use static member and set value from Position of your holder to save the value in longclick method member for example:-
好的,要解决问题 info null **** 使用静态成员并从持有人的位置设置值以将值保存在 longclick 方法成员中,例如:-
public class CurrentPosition {
public static int Pos{ get; set; }
}
public bool OnLongClick(View v)
{
CurrentPosition.Pos = Position;
return false;
}
and use in your context select item:
并在您的上下文中使用选择项:
public override bool OnContextItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case 0:
return true;
case 1:
Toast.MakeText(this,CurrentPosition.Pos.ToString(), ToastLength.Long).Show();
return true;
case 2:
Toast.MakeText(this, "Save", ToastLength.Long).Show();
return true;
}
return true;
}
}
C# code
C# 代码