2012-07-18 78 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