我有一個爲其創建上下文菜單的動態創建的TextView的列表。但是,我的代碼返回第一個文本視圖的ID而不是單擊一個。getItemId返回列表中的第一個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;
}
你使用兩次v.getId()... – njzk2