赞
踩
相信昨天看完上一篇的收藏,大家对历史记录也不陌生吧
既然是历史记录,那么就要做本地处理,那同样的我们也是用greendao来做
1.
先依赖这个implementation 'org.greenrobot:greendao:3.2.2'
apply plugin: 'org.greenrobot.greendao' // apply plugin
greendao { schemaVersion 1 daoPackage 'com.test_collect' targetGenDir 'src/main/java' }
然后再gradle中配置
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
配置完之后make project!!!
2.进来的时候是不是要先初始化
在application中做
public static DaoSession getDaoSession() { if(daoSession ==null) { helper = new MySQLiteOpenHelper(mContext, "test.db", null); SQLiteDatabase db = helper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } return daoSession; } /** * 关闭所有的操作,数据库开启后,使用完毕要关闭 */ public void closeConnection(){ closeHelper(); closeDaoSession(); } public void closeHelper(){ if(helper != null){ helper.close(); helper = null; } } public void closeDaoSession(){ if(daoSession != null){ daoSession.clear(); daoSession = null; } } public static HistoryDao getHistoryBookDao(){ return getDaoSession().getHistoryDao(); }
对了,openhelper的继承需要这个条件
implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.3'
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); } MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { @Override public void onCreateAllTables(Database db, boolean ifNotExists) { DaoMaster.createAllTables(db, ifNotExists); } @Override public void onDropAllTables(Database db, boolean ifExists) { DaoMaster.dropAllTables(db, ifExists); } }, HistoryDao.class); } }
ok基础的已经配置完毕啦,接着就是关键的啦
既然是历史记录,那肯定是得做本地,那么就得从你点击列表这一刻开始,
adapter1.setOnItemClickListener(new Recycler_variety_Adapter.OnItemClickListener() { private History history; @Override public void onItemClick(int position, Data item ) { if(item == null ||"".equals(item)){ return; } String content = item.getAddbody(); String itemId = item.getId(); // String itemId = "72711"; StringBuffer urlSb = new StringBuffer("http://baidu.com"); urlSb.append(itemId).append(".html"); String url = urlSb.toString(); List<String> images = item.getImages(); String img_url = images.get(0); String title = item.getTitle(); history = HistoryManager.getBook(title); if(history ==null){ history = new History(); history.setImg_url(img_url); history.setHtml(url); history.setContent(content); history.setTitle(title); history.setPubtime(item.getPubdate()); history.setRead(item.getClick()); history.setId1(item.getId()); // history.setIsCheck(true); } //删除已经存在重复的历史记录 List<History> list2 = HistoryManager.dao.queryBuilder() .where(HistoryDao.Properties.Title.eq(title)).build().list(); HistoryManager.dao.deleteInTx(list2); //添加 if (!title.equals("")) HistoryManager.dao.insert(history); DetailContentActivity.start(getContext()); } });
没错,就是把属于历史记录的所有信息都保存起来,然后删去重复的历史记录,我们还是以title作为主要的依据
历史记录的管理工具在如下
public class HistoryManager { public static HistoryDao dao = MyApplication.getHistoryBookDao(); /** * 得到历史记录数量 * @return */ public static int getCount(){ return (int) dao.queryBuilder().buildCount().count(); } /** * 得到所有历史记录 * @return */ public static List<History> getAllHistory(){ return dao.queryBuilder().list(); } /** * 根据title得到历史记录 * @param bookId * @return */ public static History getHistory(String title){ if(bookId != null) { return dao.queryBuilder().where(HistoryDao.Properties.Title.eq(title)) .unique(); } return null; } /** * 插入历史 * @param book */ public static void insertHistory(History book){ // dao.insert(book); dao.insertOrReplace(book); } public static void deleteHistory(){ dao.deleteAll(); } }
History的类依据大家的需求哈,
然后再历史记录的列表需要这样获取
private List<History> allBook;
/** * 获取数据来源 */ public void getList(){ allBook = HistoryManager.dao.queryBuilder().list(); if(allBook ==null ||"".equals(allBook)){ return; } }
以上就是今天的历史记录啦~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。