我已經解析了很多這裏的帖子,並沒有發現任何相當像我的問題。僅在手動調用onListItemClick中的openContextMenu時,才能在onCreateContextMenu和onContextItemSelected中選擇Android NULL menuInfo。長按工程
基本上我試圖撥openContextMenu(l)
在onListItemClick
。這樣做會創建一個沒有menuInfo
的上下文菜單。執行長按可以正常工作。在執行長按操作後,我的代碼將開始工作,並且實際上得到不爲空的menuInfo
。
我有一個ListActivity
充滿了SimpleCursorAdapter
從SQL
抓取數據。
在我的onCreate我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);
}
...