1.GreenDao工具类
public class DbUtil {
private volatile static DbUtil dbUtil;
private DailyNewsDbBeanDao dailyNewsDbBeanDao;
public static DbUtil getDbUtil() {
if (dbUtil == null) {
synchronized (DbUtil.class) {
if (dbUtil == null) {
dbUtil = new DbUtil();
}
}
}
return dbUtil;
}
private DbUtil() {
//1.创建数据库
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApplication.getApplication(), "news.db");
//2.获取读写对象
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
//3.获取管理器类
DaoSession daoSession = daoMaster.newSession();
//4.获取表对象
dailyNewsDbBeanDao = daoSession.getDailyNewsDbBeanDao();
}
/**
* 查询全部数据
*
* @return
*/
public List<DailyNewsDbBean> query() {
return dailyNewsDbBeanDao.queryBuilder().list();
}
/**
* 插入一条数据
*
* @param dailyNewsDbBean
* @return
*/
public long insert(DailyNewsDbBean dailyNewsDbBean) {
if (!isHash(dailyNewsDbBean)) {
long l = dailyNewsDbBeanDao.insertOrReplace(dailyNewsDbBean);
return l;
}
return -1;
}
/**
* 删除一条数据
*
* @param dailyNewsDbBean
* @return
*/
public boolean delete(DailyNewsDbBean dailyNewsDbBean) {
if (isHash(dailyNewsDbBean)) {
dailyNewsDbBeanDao.delete(dailyNewsDbBean);
return true;
}
return false;
}
/**
* 修改一条数据
*
* @param dailyNewsDbBean
* @return
*/
public boolean updata(DailyNewsDbBean dailyNewsDbBean) {
if (isHash(dailyNewsDbBean)) {
dailyNewsDbBeanDao.update(dailyNewsDbBean);
return true;
}
return false;
}
/**
* 查询对应的题目和id
*
* @param dailyNewsDbBean
* @return
*/
public List<DailyNewsDbBean> queryStudent(DailyNewsDbBean dailyNewsDbBean) {
return dailyNewsDbBeanDao.queryBuilder().where(DailyNewsDbBeanDao.Properties.Title.eq(dailyNewsDbBean.getTitle()), DailyNewsDbBeanDao.Properties.Id.gt(dailyNewsDbBean.getId())).list();
}
/**
* 查询多少页
*
* @param page
* @param count
* @return
*/
public List<DailyNewsDbBean> queryPage(int page, int count) {
return dailyNewsDbBeanDao.queryBuilder().offset(page * count).limit(count).list();
}
/**
* 模糊查询
*
* @param info
* @return
*/
public List<DailyNewsDbBean> querLike(String info) {
List<DailyNewsDbBean> list = dailyNewsDbBeanDao.queryBuilder().where(DailyNewsDbBeanDao.Properties.Title.like("%" + student + "%")).list();
return list;
}
/**
* 判断是否存在
*
* @param dailyNewsDbBean
* @return
*/
public boolean isHash(DailyNewsDbBean dailyNewsDbBean) {
List<DailyNewsDbBean> list = dailyNewsDbBeanDao.queryBuilder().where(DailyNewsDbBeanDao.Properties.Id.eq(dailyNewsDbBean.getId())).list();
if (list != null && list.size() > 0) {
return true;
}
return false;
}
}
2.SharedPreferencesUtil
public class SharedPreferencesUtil {
/**
* 保存在手机里面的文件名
*/
private static final String FILE_NAME = "share_date";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*
* @param context
* @param key
* @param object
*/
public static void setParam(Context context, String key, Object object) {
String type = object.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if ("String".equals(type)) {
editor.putString(key, (String) object);
} else if ("Integer".equals(type)) {
editor.putInt(key, (Integer) object);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) object);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) object);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) object);
}
editor.commit();
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*
* @param context
* @param key
* @param defaultObject
* @return
*/
public static Object getParam(Context context, String key, Object defaultObject) {
String type = defaultObject.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
if ("String".equals(type)) {
return sp.getString(key, (String) defaultObject);
} else if ("Integer".equals(type)) {
return sp.getInt(key, (Integer) defaultObject);
} else if ("Boolean".equals(type)) {
return sp.getBoolean(key, (Boolean) defaultObject);
} else if ("Float".equals(type)) {
return sp.getFloat(key, (Float) defaultObject);
} else if ("Long".equals(type)) {
return sp.getLong(key, (Long) defaultObject);
}
return null;
}
}
网友评论