SQLDatabase不会保存字符串

时间:2012-07-18 07:12:39

标签: android sqlite manifest

我目前正在制作一个简单的Android sql数据库程序。第一个活动有效,但它不保存我在编辑文本中输入的文本。这是我的代码:

public class DatabaseFinalActivity extends Activity {
    NoteAdapter adapter=null;
    NoteHelper helper=null;
    Cursor dataset_cursor=null;
    EditText editNote=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try
        {
            setContentView(R.layout.main);
            ListView list=(ListView)findViewById(R.id.listView1);
            editNote = (EditText)findViewById(R.id.editText1);

            helper=new NoteHelper(this);
            dataset_cursor=helper.getAll();
            startManagingCursor(dataset_cursor);
            adapter=new NoteAdapter(dataset_cursor);
            list.setAdapter(adapter);

            Button btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(onSave); 
        }
        catch(Exception e) {
            Log.e("Error", "Error in code: " + e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        helper.close();
    }

    private View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v){
            helper.insert(editNote.getText().toString());
            dataset_cursor.requery();
            editNote.setText("");
        }
    };

    class NoteAdapter extends CursorAdapter {
        NoteAdapter(Cursor c) {
            super(DatabaseFinalActivity.this, c);
        }

        @Override 
        public void bindView(View row, Context ctxt, Cursor c) 
        {
            NoteHolder holder=(NoteHolder)row.getTag();
            holder.populateFrom(c, helper);
        }

        @Override
        public View newView(Context ctxt, Cursor c, ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            NoteHolder holder=new NoteHolder(row);

            row.setTag(holder);
            return(row);
        }
    }

    static class NoteHolder {
        private TextView noteText=null;
        NoteHolder(View row){
            noteText=(TextView)row.findViewById(R.id.textView1);
        }

        void populateFrom(Cursor c, NoteHelper helper) {
            noteText.setText(helper.getNote(c)); 
        }
    }
}

这是我的帮手:

public class NoteHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="note.db";
    private static final int SCHEMA_VERSION=1;

    public NoteHelper(Context context) { 
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE NOTES (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int OldVersion, int NewVersion) {
    }

    public void insert (String note) {
        ContentValues cv=new ContentValues();
        cv.put("note", note);
        getWritableDatabase().insert("Notes", "note", cv);
    }

    public Cursor getAll() {
        return(getReadableDatabase().rawQuery("SELECT_id, note FROM notes", null));
    }

    public String getNote(Cursor c) {
        return(c.getString(1));
    }
}

我怀疑问题出现在清单中。这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="database.fli.one"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".DatabaseFinalActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

提前感谢您的意见和建议!

1 个答案:

答案 0 :(得分:0)

问题很简单,改变一下:

getWritableDatabase().insert("Notes", "note", cv);

对此:

getWritableDatabase().insert("Notes", null, cv);

SQLiteDatabase.insert()的中间参数是一个将空/空行插入数据库的解决方法,方法是将您告诉数据库的列note传递给该列 ContentValues中的内容。

简而言之,您告诉数据库输入一个空行。改为使用null。