我已经解析了很多帖子,但没有找到任何类似我的问题。
基本上我试图用onListItemClick
打电话给openContextMenu(l)
. 这样做会创建一个没有menuInfo
的上下文菜单。 执行长按将正常工作。 执行长按后,我的代码将开始工作,并实际获得一个不为 null 的menuInfo
。
我有一个ListActivity
,里面装满了一个从SQL
中获取数据的SimpleCursorAdapter
。
在我的创作中,我registerForContextMenu(getListView())
.我也尝试在openContextMenu(l)
通话之前使用registerForContextMenu(l)
。
任何帮助将不胜感激! 谢谢。
这是我的代码示例:
public class MY_Activity extends ListActivity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
UpdateTable();
registerForContextMenu(getListView());
}
...
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
super.onListItemClick(l, view, position, id);
//THIS DOESNT WORK UNLESS A LONG CLICK HAPPENS FIRST
//registerForContextMenu(l); //Tried doing it here too
openContextMenu(l);
//unregisterForContextMenu(l); //Then unregistering here...
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//menuInfo will be null here.
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "One");
menu.add(0, v.getId(), 0, "Two");
menu.add(0, v.getId(), 0, "Three");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(info == null) {
Log.e("","NULL context menu intem info...");
return false;
}
}
public void UpdateTable() {
cursor = DatabaseHelper_Main.GetCursor(my_id);
cursorAdapter = new SimpleCursorAdapter(this, R.layout.my_listview_entry,
cursor, fields, fieldResources, 0);
setListAdapter(cursorAdapter);
}
...
我今天遇到了一个非常相似的问题,修复程序出乎意料地简单,但我不明白为什么,但我还是会在这里发布。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
m_contextMenu = true;
registerForContextMenu(parent);
m_listView.showContextMenuForChild(view);
unregisterForContextMenu(parent);
m_contextMenu = false;
}
我使用m_contextMenu布尔值来指示正在显示上下文菜单,我有一个onItemLongClickListener,如果m_contextMenu为真则返回false,以便上下文菜单确实显示(如果onItemLongClick((返回true,它不会显示上下文菜单(。