先附上源代码。代码不断更新中,主要实现一个记事本的功能,涉及SQLite的CRUD操作。
dummynote.java
package com.example.dummynote; import android.os.Bundle; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.MenuItem; import android.R.bool; import android.app.ListActivity; import android.database.Cursor; import android.widget.ArrayAdapter; import android.widget.ListAdapter; public class DummyNote extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getListView().setEmptyView(findViewById(R.id.empty));//设置列表为空时的显示内容为 empty文本框 setAdapter(); } private String[] note_array={ /*"hangsome", "lushichuanshuo", "android", "library"*/ }; private NotesDbAdapter mDbHelper ; private Cursor mNoteCursor; private void setAdapter(){ mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); fillData(); } private void fillData(){ mNoteCursor = mDbHelper.getall(); startManagingCursor(mNoteCursor); String[] from = new String[]{NotesDbAdapter.NOTE}; int[] to = new int[]{android.R.id.text1};//android.R.id.text1是Android 框架里面的TextView的一个标识符 //Now create a simple cursor adapter SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mNoteCursor, from, to); setListAdapter(adapter); } //Add a entity private int mNoteNumber = 1; protected static final int MENU_INSERT = Menu.FIRST; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. super.onCreateOptionsMenu(menu); menu.add(0,MENU_INSERT, 0, "新增记事"); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case MENU_INSERT: String noteName = "Note "+ mNoteNumber++; mDbHelper.create(noteName); fillData(); return true; } return super.onOptionsItemSelected(item); } }<p> </p>
NotesDbAdapter.java<pre name="code" class="java">package com.example.dummynote; import java.util.Date; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.*; import android.util.Log; public class NotesDbAdapter { private static final String TAG = "notes"; private static final String DATABASE_NAME = "notes.db"; private static final int DATABASE_VERSION = 1 ; private static final String DATABASE_TABLE = "notes"; private static final String DATABASE_CREATE = "create table notes(" +"_id INTEGER PRIMARY KEY," +"note TEXT NOT NULL," +"created INTEGER" +");"; private static class DatabaseHelper extends SQLiteOpenHelper{ public DatabaseHelper(Context context) { super(context, DATABASE_NAME,null,DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override//创建数据表 public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub try { db.execSQL(DATABASE_CREATE); Log.d(TAG, "onCreate !"); } catch (Exception e) { Log.d(TAG, e.toString()); } } @Override//更新数据表 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); onCreate(db); Log.d(TAG, "inUpgrade!"); } catch (Exception e) { Log.d(TAG, "onUpgrade failure!"); } } } private Context mCtx = null; //抽象界面 private DatabaseHelper dbHelper; //数据库工具类 private SQLiteDatabase db; //数据库类 /** COnstructor**/ public NotesDbAdapter(Context ctx){ this.mCtx=ctx; } public NotesDbAdapter open () throws SQLException{ dbHelper = new DatabaseHelper(mCtx); db = dbHelper.getWritableDatabase();//数据库不存在就创造一个,若存在就根据版本库来决定是否更新数据库 return this; } public void close(){ dbHelper.close(); } public static final String ROWID = "_id"; public static final String NOTE = "note"; public static final String CREATED = "created"; String[] strCols = new String[]{ ROWID, NOTE, CREATED }; //检索。返回的是指针。少内存 public Cursor getall() { return db.query(DATABASE_TABLE,//Which table to select strCols,//Which columns to return null, //Where clause null, //Where arguments null, //Group By clause null, //Having clause null //Order-by clause ); } //增加 public long create(String Note){ Date now = new Date(); ContentValues args = new ContentValues(); args.put(NOTE, Note); args.put(CREATED, now.getDate()); return db.insert(DATABASE_TABLE, null, args); } }
时间: 2024-12-16 02:58:05