Android 闹钟 开发过程记录(二)

界面布局好了,接下来就是数据处理了。每一个闹钟设置好后,得获取用户选择的数据,并存储起来。

闹钟的数据包括:时间(小时,分钟)、重复周期、铃声、振动、标签、是否开启。





数据库表的创建代码如下:

	@Override
	public void onCreate(SQLiteDatabase db) {
		//  	id hour minute repeatCycle ring shake tag state
		String sql = "create table alarmclock (_id integer primary key autoincrement, hour integer, minute integer, "
				+ "repeatCycle varchar(20), ring varchar(20), shake integer, tag varchar(20), state integer)";
		db.execSQL(sql);
	}

建好了数据库表,就要执行对数据库的增删改查的操作了,代码如下:

public class AlarmClockDao {

	private Context context;
	private AlarmClockDBHelper dbHelper;
	public static final String ALARM_CLOCK_TABLE_NAME = "alarmclock";
	public AlarmClockDao(Context context) {
		this.context = context;
		dbHelper = new AlarmClockDBHelper(this.context);
	}

	/**
	 * 添加闹钟信息到数据库
	 * @param hour 小时
	 * @param minute 分钟
	 * @param repeatCycle 重复周期
	 * @param ring 铃声
	 * @param isShake 是否振动 是1 否0
	 * @param tag 备注
	 * @param state 闹钟状态 开1 关0
	 */
	public void insert(int hour, int minute, String repeatCycle, String ring, int isShake, String tag, int state){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			ContentValues values = new ContentValues();
			values.put("hour", hour);
			values.put("minute", minute);
			values.put("repeatCycle", repeatCycle);
			values.put("ring", ring);
			values.put("shake", isShake);
			values.put("tag", tag);
			values.put("state", state);
			db.insert(ALARM_CLOCK_TABLE_NAME, null, values);
			db.close();
		}
	}

	/**
	 * 查询所有闹钟的信息
	 * @return
	 */
	public List<AlarmClockInfo> findAll(){
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		List<AlarmClockInfo> infos = new ArrayList<AlarmClockInfo>();
		if(db.isOpen()){
			Cursor cursor = db.query(ALARM_CLOCK_TABLE_NAME, null, null, null, null, null, null);
			while(cursor.moveToNext()){
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				int hour = cursor.getInt(cursor.getColumnIndex("hour"));
				int minute = cursor.getInt(cursor.getColumnIndex("minute"));
				String repeatCycle = cursor.getString(cursor.getColumnIndex("repeatCycle"));
				String ring = cursor.getString(cursor.getColumnIndex("ring"));
				int isShake = cursor.getInt(cursor.getColumnIndex("shake"));
				String tag = cursor.getString(cursor.getColumnIndex("tag"));
				int state = cursor.getInt(cursor.getColumnIndex("state"));
				AlarmClockInfo info = new AlarmClockInfo(id, hour, minute, repeatCycle, ring, isShake, tag, state);
				infos.add(info);
			}
			cursor.close();
			db.close();
		}
		return infos;
	}


	/**
	 * 只更新指定id的state列的数据
	 * @目的
	 * 针对闹钟列表的checkbox控制闹钟开关的功能
	 * @param id 闹钟id
	 * @param state 闹钟状态 开1 关0
	 */
	public void updataState(int id,int state) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			ContentValues values=new ContentValues();
			values.put("state", state);
			db.update(ALARM_CLOCK_TABLE_NAME, values, "_id ="+ id, null);
			db.close();
		}
	}


	/**
	 * 根据闹钟的 id 更新闹钟信息
	 * @param id 闹钟id
	 * @param hour 小时
	 * @param minute 分钟
	 * @param repeatCycle 重复周期
	 * @param ring 铃声
	 * @param isShake 是否振动 是1 否0
	 * @param tag 备注
	 * @param state 闹钟状态 开1 关0
	 */
	public void update(int id, int hour, int minute, String repeatCycle, String ring, int isShake, String tag, int state){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			ContentValues values = new ContentValues();
			values.put("hour", hour);
			values.put("minute", minute);
			values.put("repeatCycle", repeatCycle);
			values.put("ring", ring);
			values.put("shake", isShake);
			values.put("tag", tag);
			values.put("state", state);
			db.update(ALARM_CLOCK_TABLE_NAME, values, "_id ="+id, null);
			db.close();
		}
	}

	/**
	 * 根据 闹钟的 id 删除此闹钟
	 * @param id
	 */
	public void delete(int id){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			db.delete(ALARM_CLOCK_TABLE_NAME, "_id ="+id, null);
			db.close();
		}
	}

	/**
	 * 查询 数据库中 最后一个闹钟 的 id
	 * @return 如果数据库中有闹钟 返回 last_id,没有返回 0
	 * @问题:假设已经有2个闹钟,此时再添加一个闹钟_id=3;再将该闹钟删除,此时该方法返回最后一个id=2;不是我想要的3。
	 * 虽然理论上没什么影响,但是不符合SQLite的主键自增长。
	 * 此外,该方法在JUnit的测试的时候,一直没反应,(+﹏+)~狂晕,如同见鬼一样令人费解。
	 * 决定采取 在插入的时候获取id  用sharedpreference 来存储 的方法
	 */
	/*
	public int findLastID(){
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		int last_id = 0;
		if(db.isOpen()){
			Cursor cursor = db.rawQuery("select _id from alarmclock", null);
			if(cursor.moveToLast()){
				last_id = cursor.getInt(cursor.getColumnIndex("_id"));
			}
			cursor.close();
			db.close();
		}
		return last_id;
	}
	 */
}

上面的 增删改查 方法,经测试都没有问题,想验证的可以自己测一下。

你可能感兴趣的:(数据库,android,闹钟)