gpt4 book ai didi

android - 对话框中的上下文菜单

转载 作者:搜寻专家 更新时间:2023-11-01 08:16:11 25 4
gpt4 key购买 nike

我有 ContextMenu 的标准代码(长按 ListView 项)但是我创建了这个类(助手对话框)但它没有注册

onContextItemSelected

方法,我不知道为什么。问题出在哪儿? =/在我的布局 XML 中,我有带 @+id/list 的常规 ListView(我没有扩展 ListActivity)。

public class CheckListDialog extends Dialog implements OnClickListener {
public CheckListDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mainContext = context;

setContentView(R.layout.dialog_check_list);

list = (ListView) findViewById(R.id.list);
registerForContextMenu(list);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
doSomething(); // This IS working.
}
});
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v == list) {

// This IS working.

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle("Something");
String[] menuItems = mainContext.getResources().getStringArray(R.array.menu);
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}

// This IS NOT working! (Method isn't called at all)
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();

String[] menuItems = mainContext.getResources().getStringArray(R.array.menu);
String menuItemName = menuItems[menuItemIndex];

if (menuItemName.equals("Delete")) {
doSomething();
}

return true;
}

@Override
public void onContextMenuClosed(Menu menu) {
doSomething();
}

private ListView list;

最佳答案

您只需实现以下功能即可。它会起作用。

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");

menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{

return true;
}
});

menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{

return true;
}
});
}
}

关于android - 对话框中的上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4579766/

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