2012-07-18 79 views
0

我目前正在做一個簡單的Android SQL數據庫程序。第一個活動起作用,但它不會保存我在編輯文本中輸入的文本。這裏是我的代碼:SQLDatabase不會保存字符串

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> 

謝謝您的意見和建議,事先!

+0

如果我是你,我會嘗試插入更多的日誌調用,以便你可以確保你寫的所有方法都被正確調用。數據庫文件(note.db)是否正確創建?它是否包含帶兩個字段的「筆記」表?您的清單文件在第一眼看起來好像很好 – Shine 2012-07-18 09:54:25

回答

0

問題很簡單,改變這種:

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

要這樣:

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

SQLiteDatabase.insert(),中間的參數是一個解決方法,以插入空/空行到你的數據庫,通過傳遞您告訴數據庫該列將爲空,而不管ContentValues中的內容爲

總之,你告訴數據庫輸入一個空行。改爲使用null。

+0

非常感謝!我會試試看。 ^^ – 2012-07-19 10:09:55