Diary DiaryDbAdapter 日记本程序

简介: riji.java public class riji extends ListActivity { private static final int ACTIVITY_CREATE = 0; private static final int ACTIVITY_EDIT = 1; private static final int INSERT_ID = Menu.FIRST;

riji.java

public class riji 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.list);
		mDbHelper = new DiaryDbAdapter(this);
		mDbHelper.open();
		renderListView();

	}

	private void renderListView() {
		mDiaryCursor = mDbHelper.getAllNotes();
		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.row, mDiaryCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.menu_insert);
		menu.add(0, DELETE_ID, 0, 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);
		i.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c
				.getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));
		i.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c
				.getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));
		startActivityForResult(i, ACTIVITY_EDIT);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		renderListView();
	}
}

ActivityDiaryEdit.java

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.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) {
			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);
				finish();
			}

		});
	}
}

DiaryDbAdapter

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 = "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);
		}

		@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 {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void closeclose() {
		mDbHelper.close();
	}

	public long createDiary(String title, String body) {
		ContentValues initialValues = new ContentValues();
		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, null, initialValues);
	}

	public boolean deleteDiary(long rowId) {

		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, null, null, null, null);
	}

	public Cursor getDiary(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
				KEY_BODY, KEY_CREATED }, KEY_ROWID + "=" + rowId, null, null,
				null, null, 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, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">

	<ListView android:id="@+id/android:list"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
	<TextView android:id="@+id/android:empty"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="按下MENU写日记!!" />
</LinearLayout>
edit.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<LinearLayout android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">

		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="@string/title"
			android:padding="2px" />
		<EditText android:id="@+id/title"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:layout_weight="1" />
	</LinearLayout>

	<TextView android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/body" />
	<EditText android:id="@+id/body" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_weight="1"
		android:scrollbars="vertical" android:gravity="top" />

	<Button android:id="@+id/confirm" android:text="@string/confirm"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

</LinearLayout>


row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/row"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<TextView android:id="@+id/text1"
		android:layout_width="wrap_content" android:layout_height="30px"
		android:maxWidth="200dip" 
		android:textSize="22sp"
		android:layout_marginTop="10dip"
		android:text="第1组第1项" />
	<TextView android:id="@+id/created" android:layout_width="wrap_content"
		android:layout_height="35px" android:layout_alignParentRight="true"
		android:layout_marginLeft="10dip" 
		android:layout_marginTop="10dip"
		android:text="2011年10月30日" />

</RelativeLayout>

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0.0" package="com.riji">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".riji"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="com.riji.ActivityDiaryEdit"/>
    </application>
</manifest> 












目录
相关文章
星际争霸之小霸王之小蜜蜂(十一)--杀杀杀
星际争霸之小霸王之小蜜蜂(十一)--杀杀杀
|
5月前
|
小程序
手机日记本小程序模板源码
手机日记本小程序模板源码
132 4
|
6月前
|
PHP
PHP开发的爱情盲盒交友系统网站源码 独立后台
PHP开发的爱情盲盒交友系统网站源码 独立后台
60 4
|
6月前
|
前端开发 JavaScript Java
计算机Java项目|网页时装购物系统
计算机Java项目|网页时装购物系统
|
C语言
无敌!我用【C语言】手搓出了一个体系完整的【员工管理系统】还能玩游戏听音乐?(超详细,附完整源码)
前言:员工管理系统是一款面向公司管理员的管理工具,旨在提供员工信息管理、薪资管理、员工绩效评估、员工福利管理和智能推荐等功能,帮助公司管理员更好地管理员工信息和提高员工工作效率。 这是总体设计大概模型图:
147 0
|
存储 缓存 JSON
小程序毕设答辩问题【论坛/校园墙小程序】
小程序毕设答辩问题【论坛/校园墙小程序】
272 0
|
SQL JavaScript Java
搭建情人节表白网站(超详细过程,包教包会)
网站的搭建其实在七夕的时候就已经弄好了,只是当时不会搭建,然后就放了好几个月,偶然发现情人节快到了,遂重新搭建了这个网站,不过说实话除了网站还真想不出有啥能够体现程序员的特长,你说各种代码,c,c++,java即时给你做出不错的GUI界面,人家还不一定会打开,网站最容易了,有手(机)就行,下面是它的效果展示视频,觉得好的记得三连哦
441 0
搭建情人节表白网站(超详细过程,包教包会)
|
开发框架 JSON 小程序
开源分享|如何解锁小程序开发?这份攻略请收好!
本期我们整理了一些用于小程序开发的开源项目,快来围观~
253 0
|
开发框架 移动开发 JavaScript
来接私活吧?小程序接私活必备功能-婚恋交友【附完整代码】
来接私活吧?小程序接私活必备功能-婚恋交友【附完整代码】
286 0
|
小程序 Linux 网络安全
崎岖坎坷小程序
0基础大三学生的小程序摸索之路。
崎岖坎坷小程序