GreenDao如何过滤数据并检索它

时间:2015-07-12 00:31:19

标签: android database calendar android-sqlite greendao

我有一个包含以下条目的表:

private Long id;
private String Date;
private String Time;
private String newEntry;
private String description;
private byte[] images;

我将它用于日历,我希望它只显示当天的条目。我该怎么做呢?

目前它检索所有条目并将其显示在listview中:

List<Box> list = BoxRepository.getAllBoxes(getApplicationContext());
    Toast.makeText(getBaseContext(), "Size = " + list.size(), Toast.LENGTH_SHORT).show();
    arrayEntry.clear();
    for (Box box : list)
        arrayEntry.add(box);
    adapterEntry.notifyDataSetChanged();
}

我的猜测是我可以在这里添加方法但是我失败了:

public static long insertOrUpdate(Context context, Box box) {
    return getBoxDao(context).insertOrReplace(box);
}

public static void clearBoxes(Context context) {
    getBoxDao(context).deleteAll();
}

public static void deleteBoxWithId(Context context, long id) {
    getBoxDao(context).delete(getBoxForId(context, id));
}

public static List<Box> getAllBoxes(Context context) {
    return getBoxDao(context).loadAll();
}

public static Box getBoxForId(Context context, long id) {
    return getBoxDao(context).load(id);
}

private static BoxDao getBoxDao(Context c) {
    return ((DatabaseManager) c.getApplicationContext()).getDaoSession().getBoxDao();
} 

1 个答案:

答案 0 :(得分:0)

我现在不知道如何格式化日期,但它应该是这样的:

helper = new DaoMaster.DevOpenHelper(this, "your database", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
boxDao= daoSession.getBoxDao();
QueryBuilder queryBuilder = boxDao.queryBuilder()
    .where(BoxDao.Properties.Date.eq("something to compare"));
List<Box> list = queryBuilder.list();

此外,您可以使用Box.Date属性的Date类型来使用Date比较器:

QueryBuilder queryBuilder = boxDao.queryBuilder()
    .where(BoxDao.Properties.Date.eq(new Date()));

QueryBuilder queryBuilder = boxDao.queryBuilder()
    .where(BoxDao.Properties.Date.between(date1, date2));