我正在创建一个带有ListView的简单对话框。我希望能够访问它的上下文菜单。这是我的基本代码:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_lista);
dialog.setTitle("Contact");
ListView lv = (ListView) dialog.findViewById(R.id.listView); Cursor cursor = db.readData();
String[] from = new String[]{DatabaseHandler.KEY_contacts, DatabaseHandler.KEY_number};
int[] to = new int[]{R.id.contacts, R.id.number};
@SuppressWarnings("deprecation")
final
SimpleCursorAdapter adapter = new SimpleCursorAdapter(Home.this, R.layout.show, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
registerForContextMenu(lv);
此处方法onCreateContextMenu:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_context, menu);
}
最后我重写了onContextItemSelected:
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(this,"call" ,Toast.LENGTH_LONG).show();
return true;
case R.id.sms:
Toast.makeText(this,"sms" ,Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
Toast.makeText(this,"delete" ,Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
我还尝试在onContextItemSelected()中覆盖setOnMenuItemClickListener()
但我的问题仍未解决:(有任何帮助吗?
答案 0 :(得分:0)
您可以使用此方法获取项目
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = lv.getItemAtPosition(position);
}
});