简体   繁体   中英

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.

This is how I delete in my 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.

    @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

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

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. 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); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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