前面已经把主界面做出来了,程序还有很重要的一部分就是数据库的搭建了
其中数据库表信息,包括表名,时间,地点,消息,是否重复闹钟,是否开启这些信息,其中一张表对应一个数据库表
db.execSQL("CREATE TABLE table_one(_id INTEGER PRIMARY KEY AUTOINCREMENT, title text, time text , location text , note text ,repeat int ,is_check int)");
数据库相关使用方法SQLHelper.java
package com.iphone.reminder.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.iphone.reminder.util.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SQLHelper {
private static String SQL_NAME = "reminder_db_test2";
private static String TABLE_NAME_ONE = "table_one";
private static String TABLE_NAME_TWO = "table_two";
private static String TABLE_NAME_THREE = "table_three";
private static String TABLE_NAME_FOUR = "table_four";
private static String TABLE_NAME = TABLE_NAME_ONE;
/**
* 创建数据库
*/
public static void createSql(Context context) {
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
@SuppressWarnings("unused")
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
// 插入数据
public static void insertSqlite(Context context, String title, String time,
String location, String note, int tableCount, int repeatNum, int isCheck) {
ContentValues values = new ContentValues();
values.put("title", title);
values.put("time", time);
values.put("location", location);
values.put("note", note);
values.put("repeat", repeatNum);
values.put("is_check", isCheck);
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert(getTableName(tableCount), null, values);
db.close();
}
// 插入单项数据
public static void insertNumber(Context context, String title,int tableCount ) {
ContentValues values = new ContentValues();
values.put("title", title);
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert(getTableName(tableCount), null, values);
db.close();
}
// 修改是否选中
public static void updateIsCheckDetail(Context context, String id,int tableCount, int isCheck) {
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("is_check", isCheck);
db.update(getTableName(tableCount), values, "_id=?", new String[]{id});
db.close();
}
// 修改
public static void updateNewDetail(Context context, String id, String newTitle, String newTime, String newLocation, String newNote, int tableCount, int newRepeatNum, int isCheck) {
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", newTitle);
values.put("time", newTime);
values.put("location", newLocation);
values.put("note", newNote);
values.put("repeat", newRepeatNum);
values.put("is_check", isCheck);
db.update(getTableName(tableCount), values, "_id=?", new String[]{id});
db.close();
}
/**
* 查,查询表中所有的数据
*/
public static List<Map<String, String>> queryAllMessage(Context context,int tableCount) {
List<Map<String, String>> tempList = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(getTableName(tableCount), null, null, null, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String _id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String time = cursor.getString(cursor.getColumnIndex("time"));
String location = cursor.getString(cursor.getColumnIndex("location"));
String note = cursor.getString(cursor.getColumnIndex("note"));
String repeat = cursor.getString(cursor.getColumnIndex("repeat"));
String isCheck = cursor.getString(cursor.getColumnIndex("is_check"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
}
db.close();
}
return tempList;
}
/**
* 在数据库中搜索关键字
*/
public static List<Map<String, String>> queryAllTitle(Context context,
String keywords) {
List<Map<String, String>> tempList = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursorOne = db.query(TABLE_NAME_ONE, null, null, null, null,
null, null);
Cursor cursorTwo = db.query(TABLE_NAME_TWO, null, null, null, null,
null, null);
Cursor cursorThree = db.query(TABLE_NAME_THREE, null, null, null, null,
null, null);
Cursor cursorFour = db.query(TABLE_NAME_FOUR, null, null, null, null,
null, null);
while (cursorOne.moveToNext()) {// 表一
String _id = cursorOne.getString(cursorOne.getColumnIndex("_id"));
String title = cursorOne.getString(cursorOne.getColumnIndex("title"));
String time = cursorOne.getString(cursorOne.getColumnIndex("time"));
String location = cursorOne.getString(cursorOne.getColumnIndex("location"));
String note = cursorOne.getString(cursorOne.getColumnIndex("note"));
String repeat = cursorOne.getString(cursorOne.getColumnIndex("repeat"));
String isCheck = cursorOne.getString(cursorOne.getColumnIndex("is_check"));
if (Utils.iskeywordsSearched(title, keywords)) {// 判斷是否符合搜索
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
} else {
}
}
while (cursorTwo.moveToNext()) {// 表二
String _id = cursorTwo.getString(cursorTwo.getColumnIndex("_id"));
String title = cursorTwo.getString(cursorTwo
.getColumnIndex("title"));
String time = cursorTwo.getString(cursorTwo.getColumnIndex("time"));
String location = cursorTwo.getString(cursorTwo
.getColumnIndex("location"));
String note = cursorTwo.getString(cursorTwo.getColumnIndex("note"));
String repeat = cursorTwo.getString(cursorTwo.getColumnIndex("repeat"));
String isCheck = cursorTwo.getString(cursorTwo.getColumnIndex("is_check"));
if (Utils.iskeywordsSearched(title, keywords)) {// 判斷是否符合搜索
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
} else {
}
}
while (cursorThree.moveToNext()) {// 表三
String _id = cursorThree.getString(cursorThree.getColumnIndex("_id"));
String title = cursorThree.getString(cursorThree
.getColumnIndex("title"));
String time = cursorThree.getString(cursorThree.getColumnIndex("time"));
String location = cursorThree.getString(cursorThree
.getColumnIndex("location"));
String note = cursorThree.getString(cursorThree.getColumnIndex("note"));
String repeat = cursorThree.getString(cursorThree.getColumnIndex("repeat"));
String isCheck = cursorThree.getString(cursorThree.getColumnIndex("is_check"));
if (Utils.iskeywordsSearched(title, keywords)) {// 判斷是否符合搜索
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
} else {
}
}
while (cursorFour.moveToNext()) {// 表四
String _id = cursorFour.getString(cursorFour.getColumnIndex("_id"));
String title = cursorFour.getString(cursorFour
.getColumnIndex("title"));
String time = cursorFour.getString(cursorFour.getColumnIndex("time"));
String location = cursorFour.getString(cursorFour
.getColumnIndex("location"));
String note = cursorFour.getString(cursorFour.getColumnIndex("note"));
String repeat = cursorFour.getString(cursorFour.getColumnIndex("repeat"));
String isCheck = cursorFour.getString(cursorFour.getColumnIndex("is_check"));
if (Utils.iskeywordsSearched(title, keywords)) {// 判斷是否符合搜索
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
} else {
}
}
db.close();
return tempList;
}
/**
* 全部4张表数据
*/
public static List<Map<String, String>> queryAllData(Context context) {
List<Map<String, String>> tempList = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursorOne = db.query(TABLE_NAME_ONE, null, null, null, null,
null, null);
Cursor cursorTwo = db.query(TABLE_NAME_TWO, null, null, null, null,
null, null);
Cursor cursorThree = db.query(TABLE_NAME_THREE, null, null, null, null,
null, null);
Cursor cursorFour = db.query(TABLE_NAME_FOUR, null, null, null, null,
null, null);
while (cursorTwo.moveToNext()) {// 表二
String _id = cursorTwo.getString(cursorTwo.getColumnIndex("_id"));
String title = cursorTwo.getString(cursorTwo
.getColumnIndex("title"));
String time = cursorTwo.getString(cursorTwo.getColumnIndex("time"));
String location = cursorTwo.getString(cursorTwo
.getColumnIndex("location"));
String note = cursorTwo.getString(cursorTwo.getColumnIndex("note"));
String repeat = cursorTwo.getString(cursorTwo.getColumnIndex("repeat"));
String isCheck = cursorTwo.getString(cursorTwo.getColumnIndex("is_check"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
}
while (cursorThree.moveToNext()) {// 表三
String _id = cursorThree.getString(cursorThree.getColumnIndex("_id"));
String title = cursorThree.getString(cursorThree
.getColumnIndex("title"));
String time = cursorThree.getString(cursorThree.getColumnIndex("time"));
String location = cursorThree.getString(cursorThree
.getColumnIndex("location"));
String note = cursorThree.getString(cursorThree.getColumnIndex("note"));
String repeat = cursorThree.getString(cursorThree.getColumnIndex("repeat"));
String isCheck = cursorThree.getString(cursorThree.getColumnIndex("is_check"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
}
while (cursorFour.moveToNext()) {// 表四
String _id = cursorFour.getString(cursorFour.getColumnIndex("_id"));
String title = cursorFour.getString(cursorFour
.getColumnIndex("title"));
String time = cursorFour.getString(cursorFour.getColumnIndex("time"));
String location = cursorFour.getString(cursorFour
.getColumnIndex("location"));
String note = cursorFour.getString(cursorFour.getColumnIndex("note"));
String repeat = cursorFour.getString(cursorFour.getColumnIndex("repeat"));
String isCheck = cursorFour.getString(cursorFour.getColumnIndex("is_check"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
}
while (cursorOne.moveToNext()) {// 表一放最后保证总表添加数据在最后
String _id = cursorOne.getString(cursorOne.getColumnIndex("_id"));
String title = cursorOne.getString(cursorOne.getColumnIndex("title"));
String time = cursorOne.getString(cursorOne.getColumnIndex("time"));
String location = cursorOne.getString(cursorOne.getColumnIndex("location"));
String note = cursorOne.getString(cursorOne.getColumnIndex("note"));
String repeat = cursorOne.getString(cursorOne.getColumnIndex("repeat"));
String isCheck = cursorOne.getString(cursorOne.getColumnIndex("is_check"));
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID", _id);
map.put("TITLE", title);
map.put("TIME", time);
map.put("LOCATION", location);
map.put("NOTE", note);
map.put("REPEAT", repeat);
map.put("ISCHECK", isCheck);
tempList.add(map);
}
db.close();
return tempList;
}
// 删除某条消息
public static void deleteDetailDate(Context context, String id, int tableCount) {
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(getTableName(tableCount), "_id=?", new String[]{id});
db.close();
}
public static void deleteAll(Context context, int tableCount) {
DatabaseHelper dbHelper = new DatabaseHelper(context, SQL_NAME);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(getTableName(tableCount), null, null);
db.close();
}
public static String getTableName(int tableCount) {
switch (tableCount) {
case 1:
TABLE_NAME = TABLE_NAME_ONE;
break;
case 2:
TABLE_NAME = TABLE_NAME_TWO;
break;
case 3:
TABLE_NAME = TABLE_NAME_THREE;
break;
case 4:
TABLE_NAME = TABLE_NAME_FOUR;
break;
}
return TABLE_NAME;
}
}
这里还没有对数据进行动态表处理,列了小于等于4张表的情况,以后在做补充
这里有一个注意点就是数据库数据是可以删除的,所以数据库数据和adapter结合的时候需要注意删除数据的情况
工具类:封装了一些要使用到的方法和上面提到的数据库和adapter使用的数据转换
package com.iphone.reminder.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.iphone.reminder.activity.TestActivity;
import com.iphone.reminder.broadcast.ReminderBroadcast;
import com.iphone.reminder.data.MessageBean;
import com.iphone.reminder.sqlite.SQLHelper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Utils {
public static final int TO_Y_VALUE = 1050;
public static final int MARGIN_GAP = 150;
public static final int BOTTOM_GAP=5;
public static final int SEY_Y = TO_Y_VALUE + MARGIN_GAP;
public static boolean isShow = false;
public static final int TABLE_COUNT_ONE = 1;
public static final int TABLE_COUNT_TWO = 2;
public static final int TABLE_COUNT_THREE = 3;
public static final int TABLE_COUNT_FOUR = 4;
public static final String TABLE_COUNT = "table_count";
public static final String TABLE_COUNT_KEY = "table_count_key";
public static final String TABLE_TITLE = "title";
public static final String TABLE_COLOR = "colors";
public static final String TABLE_TITLE_KEY = "TITLE";
public static final String TABLE_COLOR_KEY = "CURRENT_TABLE";
/**
* 判断软键盘是否弹出
*/
public static boolean isSoftShowing(Activity mActivity) {
// 获取当前屏幕内容的高度
int screenHeight = mActivity.getWindow().getDecorView().getHeight();
// 获取View可见区域的bottom
Rect rect = new Rect();
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return screenHeight - rect.bottom != 0;
}
/**
* 把数据库数据转到可以让adapter使用的ArrayList数据
*/
public static ArrayList<MessageBean> addTableOneDetailsDate(Context mContext) {
SQLHelper.createSql(mContext);
List<Map<String, String>> listTemp = SQLHelper.queryAllData(
mContext);// 调用数据库数据
ArrayList<MessageBean> mMessageList = new ArrayList<MessageBean>();
if (listTemp.size() > 0) {
for (int i = 0; i < listTemp.size(); i++) {
MessageBean item = new MessageBean();
item.title = listTemp.get(i).get("TITLE");
item.time = listTemp.get(i).get("TIME");
item.location = listTemp.get(i).get("LOCATION");
item.note = listTemp.get(i).get("NOTE");
item.repeat = Integer.valueOf(listTemp.get(i).get("REPEAT"));
item.isCheck = listTemp.get(i).get("ISCHECK").equals("0")?false:true;
mMessageList.add(item);
}
}
return mMessageList;
}
/**
* 把数据库数据转到可以让adapter使用的ArrayList数据
*/
public static ArrayList<MessageBean> addDetailsDate(Context mContext,
int tabelCount) {
SQLHelper.createSql(mContext);
List<Map<String, String>> listTemp = SQLHelper.queryAllMessage(
mContext, tabelCount);// 调用数据库数据
ArrayList<MessageBean> mMessageList = new ArrayList<MessageBean>();
if (listTemp.size() > 0) {
for (int i = 0; i < listTemp.size(); i++) {
MessageBean item = new MessageBean();
item.title = listTemp.get(i).get("TITLE");
item.time = listTemp.get(i).get("TIME");
item.location = listTemp.get(i).get("LOCATION");
item.note = listTemp.get(i).get("NOTE");
item.repeat = Integer.valueOf(listTemp.get(i).get("REPEAT"));
item.isCheck = listTemp.get(i).get("ISCHECK").equals("0")?false:true;
if(!Utils.isShow&&item.isCheck) {
}else{
mMessageList.add(item);
}
}
}
return mMessageList;
}
/**
* 把数据库数据转到可以让adapter使用的ArrayList数据
*/
public static ArrayList<MessageBean> addSearchData(Context mContext, String keywords) {
SQLHelper.createSql(mContext);
List<Map<String, String>> listTemp = SQLHelper.queryAllTitle(
mContext, keywords);// 调用数据库数据
ArrayList<MessageBean> mMessageList = new ArrayList<MessageBean>();
if (listTemp.size() > 0) {
for (int i = 0; i < listTemp.size(); i++) {
MessageBean item = new MessageBean();
item.title = listTemp.get(i).get("TITLE");
item.time = listTemp.get(i).get("TIME");
item.location = listTemp.get(i).get("LOCATION");
item.note = listTemp.get(i).get("NOTE");
item.repeat = Integer.valueOf(listTemp.get(i).get("REPEAT"));
item.isCheck = listTemp.get(i).get("ISCHECK").equals("0")?false:true;
mMessageList.add(item);
}
}
return mMessageList;
}
/**
* 一次性闹钟
*/
public static void singleReminder(Activity activity, Context context,
String title, String note, long timeStamp, String currentTime) {
Intent intent = new Intent(activity, ReminderBroadcast.class);
intent.setAction("VIDEO_TIMER");
intent.putExtra("TITLE", title);
intent.putExtra("NOTE", note);
intent.putExtra("CTIME", currentTime);
PendingIntent sender = PendingIntent.getBroadcast(activity, 0, intent,
0);
AlarmManager am = (AlarmManager) context
.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeStamp,
sender);
}
/**
* 重复闹钟
*/
public static void alarmRepeatReminder(Activity activity, Context context,
String title, String note, long timeStamp, long intervalMillis, String currentTime) {
Intent intent = new Intent(activity, ReminderBroadcast.class);
intent.setAction("VIDEO_TIMER");
intent.putExtra("TITLE", title);
intent.putExtra("NOTE", note);
intent.putExtra("CTIME", currentTime);
Log.d("cfb","utils===="+title+"---note=="+note+"````currentTime---"+currentTime);
// PendingIntent这个类用于处理即将发生的事情
PendingIntent sender = PendingIntent.getBroadcast(activity, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
@SuppressWarnings("static-access")
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
timeStamp, intervalMillis, sender);
}
/**
* 将字符串转为时间戳
*/
@SuppressLint("SimpleDateFormat")
public static String getTime(String user_time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(user_time);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
return re_time;
}
/**
* 判断搜索条件
*/
public static boolean iskeywordsSearched(String titleStr, String keywords) {
if (titleStr.indexOf(keywords) != -1) {
return true;
} else {
return false;
}
}
public static boolean isValidTime(long date1, long date2) {
//SimpleDateFormat currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean isValidTime = false;
//try {
//double beginTime = currentTime.parse(date1);
//double endTime = currentTime.parse(date2);
if(date2-date1>0){
isValidTime = true;
}else{
isValidTime = false;
}
Log.d("cfb","isValidTime==="+isValidTime);
/*} catch (java.text.ParseException e) {
e.printStackTrace();
}*/
return isValidTime;
}
//判断当前软键盘状态
public static boolean isHideOrShowImm(View v){
InputMethodManager imm = (InputMethodManager) TestActivity.mTestActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if(imm.hideSoftInputFromWindow(v.getWindowToken(), 0))
{
//软键盘已弹出
return true;
}
else
{
//软键盘未弹出
return false;
}
}
// 隐藏输入法
public static void hideImm(){
InputMethodManager imm = (InputMethodManager) TestActivity.mTestActivity
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
所以有在建表的时候加入数据
ListStyleView mListStyleView = new ListStyleView(getApplicationContext(), Utils.addDetailsDate(getApplicationContext(), mListStyleViewList.size() + 1), mListStyleViewList.size() + 1);
直接将数据传入ListStyleView 表中,再将该数据放入adapter中:
mYAdpter.setmMessageItems(mMessageList);
MessageBean item = mMessageItems.get(position);
MessageBen 中就是get/set数据库中的信息
package com.iphone.reminder.data;
import com.iphone.reminder.listview.SlideView;
public class MessageBean {
private int id;
public String title;
public String time;
public String location;
public String note;
public int repeat;
public boolean isCheck;
public SlideView slideView;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLocation() {
return location;
}
public int getRepeat(int repeat){
return repeat;
}
public void setLocation(String location) {
this.location = location;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public SlideView getSlideView() {
return slideView;
}
public void setSlideView(SlideView slideView) {
this.slideView = slideView;
}
}
到这里我们就可以看到主界面的数据了。