点击添加后自动添加2次

时间:2013-09-15 05:14:05

标签: android sqlite bundle

我试着做一个简单的说明。 点击保存后添加新笔记,他们保存为2注意相同的内容。这意味着他们节省了2倍。我不知道为什么。

在noteadapter类中创建注释:

    public long createNote(String title, String body, String date) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_BODY, body);
    initialValues.put(KEY_DATE, date);

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

在notelist类中调用create notes:

    private void createNote() {
    Intent i = new Intent(this, NoteEdit.class);
    startActivityForResult(i, ACTIVITY_CREATE);    

和noteedit类中的事件添加编辑或del记录:

    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

}

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_about:
        case R.id.menu_delete:
            if(note != null){
                note.close();
                note = null;
            }
            if(mRowId != null){
                mDbHelper.deleteNote(mRowId);
            }
            finish();

            return true;
        case R.id.menu_save:
            saveState();
            finish();           
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void saveState() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();

        if(mRowId == null){
            mDbHelper.createNote(title, body, curDate);
        }else{
            mDbHelper.updateNote(mRowId, title, body, curDate);
        }
    }


    private void populateFields() {
        if (mRowId != null) {
            note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
            mTitleText.setText(note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
            mBodyText.setText(note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
            curText = note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
        }
    }

这里可能有问题,当我尝试获取rowID时:

mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

我的完整代码:

http://www.mediafire.com/download/w1kyy7spc522za9/Notepad.zip

1 个答案:

答案 0 :(得分:0)

onOptionsItemSelected()中,您正在呼叫saveState()以保存该记事,然后finish()该活动。 (之后您可能应该return true;停止进一步处理选项项目选择,但这不是问题。)当活动结束时,它将进入暂停状态。在onPause()中,您再次致电saveState()saveState()mRowId定义时更新了一个条目,但您只在onCreate()中设置,从而在数据库中创建了一个新行。

解决方案是捕获插入的行id并在执行第一次插入时将其存储,以便稍后调用saveState()来更新行。您的db帮助器createNote()已经返回了您未存储的行ID。