我有一个动态创建的 TextView 列表,我正在为其创建上下文菜单。但是,我的代码返回第一个文本视图的 id,而不是单击的那个。
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Create your context menu here
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Edit n Replace");
menu.add(0, v.getId(), 1, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// Call your function to preform for buttons pressed in a context menu
// can use item.getTitle() or similar to find out button pressed
// item.getItemID() will return the v.getID() that we passed before
super.onContextItemSelected(item);
if ( item.getTitle().toString().equals("Delete")){
NotesDatabase db =new NotesDatabase(this);
TextView tv = (TextView) findViewById(item.getItemId());
db.searchAndDelete(tv.getText().toString());
}
return true;
}