GreenDao使用小记

首先是bean类

/**
* Created by HJR36 on 2016/11/2.
* 互动消息bean类
*/
@Entity(
nameInDb = “message_db”
)
public class ChangemessageBean {
/**
* circleText :
* targetType : 1
* circleId : 9234823812341234
* dynamicType : 1
* userIcon : http://www.baidu.com/asdf.jpg
* userName : 切克闹
* circleImgUrl : http://www.baidu.com
* userId : us2134234
* commentContent :
*/
@Id(autoincrement = true)
@Index(unique = true) // 建立索引
private Long messageId;//消息Id
/*圈子文字内容/
public String circleText;
public int targetType;
public String circleId;
public int dynamicType;//活动圈类型
public String circleImgUrl;
public String userName;
public String commentContent;
public String dataTime;
public String userImg;
public boolean isLook=false;
}
(写好之后,点击编译,数据库会自动生成表)

数据库管理类
/**
* Created by HJR36 on 2016/11/2.
* 互动消息的数据库管理类
*/

public class ChangeMessageManager {
private ChangemessageBeanDao mChangemessageBeanDao;
private static ChangeMessageManager changeMessageManager = null;
private static Context context;

private ChangeMessageManager(Context mContext) {
    try {
        mChangemessageBeanDao = HtDBManager.getInstance(mContext).getDaoSession().getChangemessageBeanDao();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static ChangeMessageManager getInstance(Context mContext) {
    context=mContext;
    if (changeMessageManager == null) {
        changeMessageManager = new ChangeMessageManager(context);
    }

    return changeMessageManager;
}

/**
 * 查询所有的互动消息
 *
 * @return
 */
public List<ChangemessageBean> loadAllChangeMessage() {
    List<ChangemessageBean> list=mChangemessageBeanDao.loadAll();
    //按时间倒序排序
    Collections.reverse(list);
    return list;
}

/**
 * 分页查询所有的互动消息
 * @return
 */
public List<ChangemessageBean> loadAllChangeMessageAsPage(int page) {
    QueryBuilder qb = mChangemessageBeanDao.queryBuilder();
    List<ChangemessageBean> list = qb
            .orderDesc(ChangemessageBeanDao.Properties.MessageId).offset((page-1) * 10).limit(10).list();
    return list;
}

/**
 * 删除所有的互动消息
 */
public void deleteChangeMessageList() {
    mChangemessageBeanDao.deleteAll();
}

/**
 * 通过 Id 查找互动消息
 *
 * @return
 */
public ChangemessageBean findChangemessageById(long id) {
    return mChangemessageBeanDao.queryBuilder().where(ChangemessageBeanDao.Properties.MessageId.eq(id)).unique();
}

/**
 * 插入数据,并返回id
 *
 * @param changemessage
 * @return
 */
public long insertOrReplaceChangemessage(ChangemessageBean changemessage) {
    return mChangemessageBeanDao.insertOrReplace(changemessage);
}

/**
 * 判断是否存在
 *
 * @param longid
 */
public boolean judgeExist(long longid) {
    ChangemessageBean changemessageBean = findChangemessageById(longid);
    if (changemessageBean != null) {
        return true;
    }
    return false;
}

/**
 * 用过 Id 删除互动消息
 *
 * @param longid
 */
public void delete(long longid) {
    ChangemessageBean changemessage = mChangemessageBeanDao
            .queryBuilder().where(ChangemessageBeanDao.Properties.MessageId.eq(longid)).unique();
    if (changemessage != null) {
        mChangemessageBeanDao.delete(changemessage);
    }
}

}

插入操作
//处理消息部分
ChangemessageBean changemessage=new ChangemessageBean();
changemessage.setCommentContent(commentContent);
changemessage.setCircleText(circleContent);
changemessage.setCircleId(targetId);
changemessage.setCircleImgUrl(imageUrl);
changemessage.setTargetType(Integer.parseInt(type));
changemessage.setDataTime(dateTime);
//处理用户部分
changemessage.setUserName(userName);
changemessage.setUserImg(userImg);
changeMessageManager.insertOrReplaceChangemessage(changemessage);

从数据库读取数据(做了分页查询)

/**
* 从本地数据库拿通知
*/
public void getChangeMesssage() {
if (currentPage == 1) {
swipeRefreshLayout.setRefreshing(false);
} else {
mCompatlv.stopLoadMore();
}
changemessageList = changeMessageManager.loadAllChangeMessageAsPage(currentPage);
if (changemessageList!=null && changemessageList.size() > 0){
mNull.setVisibility(View.GONE);
if (currentPage == 1) {
mAdapter.clearAll();
mAdapter.add(changemessageList);
} else {
mAdapter.add(changemessageList);
}
currentPage++;
if (changemessageList.size() < 10) {
mCompatlv.setPullLoadEnable(false);
} else {
mCompatlv.setPullLoadEnable(true);
}
}else {
if (currentPage == 1) {
displayNulContent();
}
}
mAdapter.notifyDataSetChanged();
}
成功截图

你可能感兴趣的:(android)