从ContentValues插入数据库不起作用

时间:2015-10-25 16:45:10

标签: android sqlite

private EditText etWord;
private EditText etTranslation;
private Switch engUsrb;
private FloatingActionButton fab;

private RjecnikDB dbRjecnik; // class that extends SQLiteOpenHelper
private SQLiteDatabase db;
private ContentValues values;

private String word;
private String translation;
private boolean eng;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);

    ////////// findViewById //////////
    etWord = (EditText) findViewById(R.id.etWord);
    etTranslation = (EditText) findViewById(R.id.etTranslation);
    engUsrb = (Switch) findViewById(R.id.engUsrb);
    fab = (FloatingActionButton) findViewById(R.id.fabSaveWord);

    word = etWord.getText().toString();
    translation = etTranslation.getText().toString();
    eng = engUsrb.isChecked();

    dbRjecnik = new RjecnikDB(this);
    db = dbRjecnik.getWritableDatabase();

    ////////// Adding to database //////////
    values = new ContentValues();
    values.put(RjecnikDB.COLUMN_WORD, word);
    values.put(RjecnikDB.COLUMN_TRANSLATION, translation);
    values.put(RjecnikDB.COLUMN_ENGUSRP, eng);

    ///////// Floating Action Button Listener //////////
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db.insert(RjecnikDB.TABLE, null, values);

            etWord.setText("");
            etTranslation.setText("");
            etWord.requestFocus();
        }
    });
}

我无法弄清楚为什么这会在表中创建新行,但它对于两个EditText视图始终为null值。 如果我在XML格式的EditText中添加文本标签,例如android:text="default",有效,可以按预期创建值为“default”的新行,但从EditText视图中获取文本无法正常工作。

2 个答案:

答案 0 :(得分:1)

您在编写任何内容之前就过早地检索了edittexts的内容。

getText().toString()方法中移动onClick()

答案 1 :(得分:0)

试试这个:

 private EditText etWord;
private EditText etTranslation;
private Switch engUsrb;
private FloatingActionButton fab;

private RjecnikDB dbRjecnik; // class that extends SQLiteOpenHelper
private SQLiteDatabase db;
private ContentValues values;

private String word;
private String translation;
private boolean eng;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);

    ////////// findViewById //////////
    etWord = (EditText) findViewById(R.id.etWord);
    etTranslation = (EditText) findViewById(R.id.etTranslation);
    engUsrb = (Switch) findViewById(R.id.engUsrb);
    fab = (FloatingActionButton) findViewById(R.id.fabSaveWord);

    dbRjecnik = new RjecnikDB(this);
    db = dbRjecnik.getWritableDatabase();

    ///////// Floating Action Button Listener //////////
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            word = etWord.getText().toString();
            translation = etTranslation.getText().toString();
            eng = engUsrb.isChecked();
            ////////// Adding to database //////////
            values = new ContentValues();
            values.put(RjecnikDB.COLUMN_WORD, word);
            values.put(RjecnikDB.COLUMN_TRANSLATION, translation);
            values.put(RjecnikDB.COLUMN_ENGUSRP, eng);

            db.insert(RjecnikDB.TABLE, null, values);

            etWord.setText("");
            etTranslation.setText("");
            etWord.requestFocus();
        }
    });
}

请勿忘记在使用

后关闭数据库