0

我目前正在制作一个简单的 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>

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

4

1 回答 1

0

问题很简单,改一下:

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

对此:

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

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

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

于 2012-07-18T15:01:16.443 回答