简体   繁体   English

如何通过菜单删除项目?

[英]How do I delete an item through the menu?

Below is the code for my submenu buttons and I'm trying to make it delete the note and return to the main list view. 下面是我的子菜单按钮的代码,我正在尝试使其删除注释并返回主列表视图。 The delete option is called "Red" for now. 删除选项现在称为“红色”。

I copied my delete code from my main activity thinking it would work, but it does not. 我从我的主要活动中复制了删除代码,以为它可以工作,但是没有用。 I'm very new to android coding, so help would be appreciated. 我对android编码非常陌生,因此将不胜感激。

This is how I delete in my Main Activity.java 这就是我在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);
}

Here is my code for my NoteEditorActivity.java Again I'm trying to delete, but I can't seem to figure out how to delete the note from the submenu. 这是我的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);
    }

Put break statements in your switch case 在您的切换案例中放置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;
    }

Try reading this here also: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 也可以尝试在此处阅读以下内容: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

its just weird..you aren't calling notesList.add() method anywhere in your code, so I just think it's empty at all.. you are certainly missing the break statement there, but I guess it is not the issue why your note is not deleted after clicking in the menu item. 它只是很notesList.add() ..您没有在代码中的任何地方调用notesList.add()方法,所以我只是认为它根本是空的..您肯定在那里缺少break语句,但是我想这不是您的原因单击菜单项后,便笺不会被删除。 You are saving your note in the "previous" (in terms of backstack) activity? 您是将笔记保存在“上一个”(按后退堆栈)活动中吗? if so, you might try to just alter the return code for the setActivityResult() call (or add some extras to the intent) and then check for it in your onActivityResult() callback.. because right now everytime you close the activity via back key, the notes is saved (the saveAndFinish() method gets called); 如果是这样,您可能会尝试仅更改setActivityResult()调用的返回代码(或在意图中添加一些其他内容),然后在onActivityResult()回调中进行检查。.因为现在您每次通过back关闭活动键,便笺被保存( saveAndFinish()方法); please describe better where you actually do save notes (to DB or so) and where you wanna delete them..I could then provide you with some code snippet probably. 请更好地描述您实际在哪里保存注释(到数据库等)以及要删除它们的位置..然后我可能会为您提供一些代码片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM