我使用以下代码为listview设置上下文菜单。现在我想为该上下文菜单创建onContextItemClickListener。显然没有像listview.setOnContextItemClickListener这样的方法。请帮助我。
ListView listView = new ListView(getApplicationContext());
@SuppressWarnings("unchecked")
ArrayAdapter listViewArrayAdapter = new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, locations);
listView.setAdapter(listViewArrayAdapter);
listView.setFocusableInTouchMode(true);
listView.setOnFocusChangeListener(
new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
Log.i("SampleApp", "onFocusChanged() - view=" + arg0);
}
});
listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view,
int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Log.i("SampleApp", "Click on position"+selectedPosition);
}
});
listView.setOnCreateContextMenuListener(
new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
menu.add(0, 0, 0, "Delete");
}
});
答案 0 :(得分:1)
你应该添加:
// we register for the contextmneu
registerForContextMenu(lv);
注册上下文菜单(即lv是ListView的实例),然后:
// This method is called when user selects an Item in the Context menu
@Override
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
// do your logic here
return true;
}
希望这就是你要找的东西。 PS:如果你有兴趣我写了一篇关于它的帖子,看看here