GreenDao 如何过滤和检索数据

GreenDao how to filter data and retrieve it

我有一个 table,其中包含以下条目:

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

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

目前它检索所有条目并将其显示在列表视图中:

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();
} 

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

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 属性 使用日期类型,以便使用日期比较器:

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

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