我创建了一个onCreateContextMenu(),通过从listview中获取值来执行删除和编辑功能。在我的代码中,删除功能和编辑功能都运行良好。但我的问题是,当我尝试从列表视图进行编辑时,它不会从我选择的内容中传递值。它总是传递我的列表视图中的第一个细节。在类似的问题中,当我通过使用“TextView updateName =(TextView)view.findViewById(R.id.lblLUnitName);”传递textview的值时,这已得到解决。但此时我无法通过“视图”来从textview中检索特定值。我怎么能改变这个?
//creating the longtimeClicked bar when loading the app
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderIcon(R.drawable.pencil_icon);
menu.setHeaderTitle(" Unit Options");
menu.add(Menu.NONE,EDIT,Menu.NONE, " Edit Unit");
menu.add(Menu.NONE, DELETE, Menu.NONE, " Delete Unit");
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE:
dbHandler.deleteUnits(Unit.get(longClickedItemIndex));
Unit.remove(longClickedItemIndex);
unitsAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Unit has been deleted.", Toast.LENGTH_LONG).show();
break;
case EDIT:
TextView updateName = (TextView)findViewById(R.id.lblLUnitName);
String updateName1 = updateName.getText().toString();
TextView updateBulb = (TextView) findViewById(R.id.lblLNoofBulbs);
// String updateBulb1=updateBulb.getText().toString();
TextView updateFan = (TextView) findViewById(R.id.lblLNoOfFans);
//String updateFan1=updateFan.getText().toString();
Intent intent_valueUpdate = new Intent(CreateNewUnit.this,
UpdateUnit.class);
int updateBulb1 = Integer.valueOf(updateBulb.getText().toString());
int updateFan1 = Integer.valueOf(updateFan.getText().toString());
// intent_valueBulb.putExtra("KeyValueA",2);
intent_valueUpdate.putExtra("keyNameA", updateName1);
intent_valueUpdate.putExtra("keyBulb1", updateBulb1);
intent_valueUpdate.putExtra("keyFan1", updateFan1);
startActivity(intent_valueUpdate);
break;
}
return super.onContextItemSelected(item);
}
答案 0 :(得分:1)
根据Android文档@ Menu class,您需要调用registerForContextMenu
方法,就像在onCreate
()中一样。搜索文本"创建浮动上下文菜单"。上下文菜单会在长项目点击时触发。
示例代码:
protected void onCreate(Bundle savedInstanceState) {
registerForContextMenu(listview);
...
}