[英]How do I delete an item through the menu?
下面是我的子菜单按钮的代码,我正在尝试使其删除注释并返回主列表视图。 删除选项现在称为“红色”。
我从我的主要活动中复制了删除代码,以为它可以工作,但是没有用。 我对android编码非常陌生,因此将不胜感激。
这就是我在Main Activity.java中删除的方式
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
currentNoteId = (int)info.id;
menu.add(0, MENU_DELETE_ID, 0, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == MENU_DELETE_ID) {
Noteitem note = notesList.get(currentNoteId);
datasource.remove(note);
refreshDisplay();
}
return super.onContextItemSelected(item);
}
这是我的NoteEditorActivity.java的代码。我再次尝试删除,但似乎无法弄清楚如何从子菜单中删除该笔记。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_exit:
EditText et = (EditText)findViewById(R.id.noteText);
if (et.length() > 0) {
saveAndFinish();
}
else
{
finish();
}
case R.id.menu_red:
currentNoteId = (int) MENU_DELETE_ID;
datasource.remove(note);
return true;
default:
return super.onOptionsItemSelected(item);
}
在您的切换案例中放置break语句
switch (item.getItemId())
{
case R.id.action_exit:
EditText et = (EditText)findViewById(R.id.noteText);
if (et.length() > 0){
saveAndFinish();
}else{
finish();
}
//you are missing this!!!
break;
case R.id.menu_red:
datasource.remove(note);
finish();
break;
default:
return super.onOptionsItemSelected(item);
break;
}
也可以尝试在此处阅读以下内容: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
它只是很notesList.add()
..您没有在代码中的任何地方调用notesList.add()
方法,所以我只是认为它根本是空的..您肯定在那里缺少break语句,但是我想这不是您的原因单击菜单项后,便笺不会被删除。 您是将笔记保存在“上一个”(按后退堆栈)活动中吗? 如果是这样,您可能会尝试仅更改setActivityResult()
调用的返回代码(或在意图中添加一些其他内容),然后在onActivityResult()
回调中进行检查。.因为现在您每次通过back关闭活动键,便笺被保存( saveAndFinish()
方法); 请更好地描述您实际在哪里保存注释(到数据库等)以及要删除它们的位置..然后我可能会为您提供一些代码片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.