这是一个简单的androID日记程序,按下菜单栏后,会出现添加新日记和删除日记两项,点击主页面的日记可以直接编辑日记
整个程序分成三个文件,主程序逻辑是ActivityMain.java,编辑逻辑是ActivityDiaryEdit.java,还有一个DiaryDbAdapter.java文件定义了一个DiaryDbAdapter类,封装数据库的 *** 作.
以下是ActivityMain.java文件的code:
/***ActivityMain.java*/import androID.app.ListActivity;import androID.content.Intent;import androID.database.Cursor;import androID.os.Bundle;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.ListVIEw;import androID.Widget.SimpleCursorAdapter;public class ActivityMain extends ListActivity { private static final int ACTIVITY_CREATE = 0; private static final int ACTIVITY_EDIT = 1; private static final int INSERT_ID = Menu.FirsT; private static final int DELETE_ID = Menu.FirsT + 1; private DiaryDbAdapter mDbHelper; // *** 作数据库的实例 private Cursor mDiaryCursor; //存储日记 @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.diary_List); mDbHelper = new DiaryDbAdapter(this); //创建自定义类DiaryDbAdapter的实例 mDbHelper.open(); //打开数据库 renderListVIEw(); //刷新显示主界面 } private voID renderListVIEw() { mDiaryCursor = mDbHelper.getAllNotes(); //获得所有日志文件,并赋给Cursor实例 startManagingCursor(mDiaryCursor); String[] from = new String[] { DiaryDbAdapter.KEY_Title,DiaryDbAdapter.KEY_CREATED }; int[] to = new int[] { R.ID.text1,R.ID.created }; SimpleCursorAdapter notes = new SimpleCursorAdapter(this,R.layout.diary_row,mDiaryCursor,from,to); //把游标数据显示在视图上的指定位置 //R.layout.diary_row为每一行的布局 setlistadapter(notes); //显示界面 } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0,INSERT_ID,R.string.menu_insert); //加入菜单项,添加日记 menu.add(0,DELETE_ID,R.string.menu_delete); //加入菜单项,删除日记 return true; } @OverrIDe public boolean onMenuItemSelected(int featureID,MenuItem item) { switch (item.getItemID()) { case INSERT_ID: createDiary(); //点击添加日记时的动作 return true; case DELETE_ID: mDbHelper.deleteDiary(getListVIEw().getSelectedItemID()); //点击删除日记时的动作 renderListVIEw(); return true; } return super.onMenuItemSelected(featureID,item); } private voID createDiary() { Intent i = new Intent(this,ActivityDiaryEdit.class); startActivityForResult(i,ACTIVITY_CREATE); //跳转到编辑界面 } @OverrIDe /* 需要对position和ID进行一个区分 * position指的是点击的这个VIEwItem在当前ListVIEw中的位置 * 每一个和VIEwItem绑定的数据,肯定都有一个ID,通过这个资源ID可以找到那条数据。 */ protected voID onListItemClick(ListVIEw l,VIEw v,int position,long ID) { //点击日记时打开编辑界面,直接编辑日记 super.onListItemClick(l,v,position,ID); Cursor c = mDiaryCursor; c.movetoposition(position); //游标移动到你点击的位置,即获得日记的数据 Intent i = new Intent(this,ActivityDiaryEdit.class); i.putExtra(DiaryDbAdapter.KEY_ROWID,ID); //把ID值存入intent i.putExtra(DiaryDbAdapter.KEY_Title,c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_Title))); //把Title的值存入intent i.putExtra(DiaryDbAdapter.KEY_BODY,c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY))); //把body的值存入intent startActivityForResult(i,ACTIVITY_EDIT); } @OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent intent) { super.onActivityResult(requestCode,resultCode,intent); renderListVIEw(); //编辑完成返回后,刷新显示主界面 }}
以下是ActivityDiaryEdit.java的code:
/***ActivityDiaryEdit.java*/import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;public class ActivityDiaryEdit extends Activity { private EditText mTitleText; private EditText mBodyText; private Long mRowID; private DiaryDbAdapter mDbHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDbHelper = new DiaryDbAdapter(this); mDbHelper.open(); setContentVIEw(R.layout.diary_edit); mTitleText = (EditText) findVIEwByID(R.ID.Title); mBodyText = (EditText) findVIEwByID(R.ID.body); button confirmbutton = (button) findVIEwByID(R.ID.confirm); mRowID = null; // 每一个intent都会带一个Bundle型的extras数据。 Bundle extras = getIntent().getExtras(); if (extras != null) { //如果extras为空,则是创建日记; 如果非空,则是编辑日记 String Title = extras.getString(DiaryDbAdapter.KEY_Title); String body = extras.getString(DiaryDbAdapter.KEY_BODY); mRowID = extras.getLong(DiaryDbAdapter.KEY_ROWID); if (Title != null) { mTitleText.setText(Title); //显示需要编辑的日记题目 } if (body != null) { mBodyText.setText(body); //显示需要编辑的日记内容 } } confirmbutton.setonClickListener(new VIEw.OnClickListener() { //保存按钮 public voID onClick(VIEw vIEw) { String Title = mTitleText.getText().toString(); String body = mBodyText.getText().toString(); if (mRowID != null) { //如果为空,则是更新日记 mDbHelper.updateDiary(mRowID,Title,body); // *** 作数据库,更新日记内容 } else mDbHelper.createDiary(Title,body); // *** 作数据库,创建日记 Intent mIntent = new Intent(); setResult(RESulT_OK,mIntent); //设置返回RESulE_OK finish(); } }); }}
以下是DiaryDbAdapter.java的code:
/***DiaryDbAdapter.java*/import java.util.Calendar;import java.util.Date;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.util.Log;public class DiaryDbAdapter { public static final String KEY_Title = "Title"; public static final String KEY_BODY = "body"; public static final String KEY_ROWID = "_ID"; public static final String KEY_CREATED = "created"; private static final String TAG = "DiaryDbAdapter"; private DatabaseHelper mDbHelper; //负责创建,打开,升级,关闭数据库 private sqliteDatabase mDb; //负责增,删,改,查等数据库的基本 *** 作 private static final String DATABASE_CREATE_table = "create table diary (_ID integer primary key autoincrement," + "Title text not null,body text not null,created text not null);"; private static final String DATABASE_name = "database"; private static final String DATABASE_table = "diary"; private static final int DATABASE_VERSION = 1; private final Context mCtx; private static class DatabaseHelper extends sqliteOpenHelper { DatabaseHelper(Context context) { super(context,DATABASE_name,null,DATABASE_VERSION); } @OverrIDe public voID onCreate(sqliteDatabase db) { db.execsql(DATABASE_CREATE_table); //创建数据表 } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { db.execsql("DROP table IF EXISTS diary"); //通过直接删除旧数据库,重建新数据库,实现数据库升级 onCreate(db); } } public DiaryDbAdapter(Context ctx) { this.mCtx = ctx; } public DiaryDbAdapter open() throws sqlException { //打开数据库,获得sqliteDatabase实例 mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public voID close() { mDbHelper.close(); //关闭数据库 } public long createDiary(String Title,String body) { ContentValues initialValues = new ContentValues(); //ContentValues与map类似,只能存储基本类型的值 initialValues.put(KEY_Title,Title); initialValues.put(KEY_BODY,body); Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" + calendar.get(Calendar.HOUR_OF_DAY) + "时" + calendar.get(Calendar.MINUTE) + "分"; initialValues.put(KEY_CREATED,created); //存储创建日记的时间 return mDb.insert(DATABASE_table,initialValues); } public boolean deleteDiary(long rowID) { //删除指定ID的日记 return mDb.delete(DATABASE_table,KEY_ROWID + "=" + rowID,null) > 0; } public Cursor getAllNotes() { return mDb.query(DATABASE_table,new String[] { KEY_ROWID,KEY_Title,KEY_BODY,KEY_CREATED },null); } public Cursor getDiary(long rowID) throws sqlException { Cursor mCursor = mDb.query(true,DATABASE_table,null); if (mCursor != null) { mCursor.movetoFirst(); } return mCursor; } public boolean updateDiary(long rowID,String Title,String body) { ContentValues args = new ContentValues(); args.put(KEY_Title,Title); args.put(KEY_BODY,body); Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" + calendar.get(Calendar.HOUR_OF_DAY) + "时" + calendar.get(Calendar.MINUTE) + "分"; args.put(KEY_CREATED,created); return mDb.update(DATABASE_table,args,null) > 0; //更新指定ID的日记 }}
最后来一张实现图,右上角是菜单项,白色部分是每个日记的标题.
总结以上是内存溢出为你收集整理的SQLite学习实例, 日记本全部内容,希望文章能够帮你解决SQLite学习实例, 日记本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)