greendao实现本地分页查询和模糊查询

	public static       int    PageSize              = 15; //分页加载
	 //无网络本地
	            QueryBuilder builder = MyApplication.getInstance().getDaoSession().getVisitorModelDao()
	                    .queryBuilder();
	            List coditions = buildQuery();
	            if (coditions != null && coditions.size() > 0) {
	                WhereCondition[] conditions2 = new WhereCondition[coditions.size()];
	                for (int i = 0; i < coditions.size(); i++) {
	                    conditions2[i] = coditions.get(i);
	                }
	                builder.where(new WhereCondition.StringCondition("1=1"), conditions2);
	            }
	            long totalCount = builder.count();
	            List list = builder.orderAsc(VisitorModelDao.Properties.State)
	                    .orderDesc(VisitorModelDao.Properties.BeginDate)
	                    .offset((pageIndex - 1) * Constant.PageSize).limit(Constant.PageSize).list();
	            Log.e("TTTT", "getData: " + list.size());
	            for (int i = 0; i < list.size(); i++) {
	                VisitorModel v = list.get(i);
	                Log.d("visitorModel", "  " + v.VisitorName + "  " + v.getStaffName() + " " + v.getOrginazitionName()
	                        + " " + v.BeginDate + " " + v.LastTime);
	            }
	            if (list != null && list.size() > 0) {
	                pager.setTotalCount((int) totalCount);
	                adapter.setList(list);
	                mTvNumber.setText((int) totalCount + "");
	                dataSetObserver.onChanged();
	            } else {
	                adapter.setList(null);
	                pager.setTotalCount(0);
	                dataSetObserver.onChanged();
	            }
//         
private List buildQuery() {
        String BeginDate = mTvLaiTime.getText().toString().trim();
        String EndDate = mTvJshuTime.getText().toString().trim();
        String VisitorName = mEdtComeName.getText().toString().trim();
        String StaffName = mEdtBeName.getText().toString().trim();
        String OrginazitionName = mEdtBeDemp.getText().toString().trim();
        int state = FormStatus.VistorStatus(mSpStatus.getSelectedItem().toString());
        List coditions = new ArrayList<>();
        if (!TextUtils.isEmpty(BeginDate)) {
            BeginDate += " 00:00:00";
        }
        if (!TextUtils.isEmpty(EndDate)) {
            EndDate += " 23:59:59";
        }
        coditions.add(VisitorModelDao.Properties.BeginDate.ge(getTime(BeginDate)));
        coditions.add(VisitorModelDao.Properties.BeginDate.le(getTime(EndDate)));
        if (!TextUtils.isEmpty(VisitorName)) {
            coditions.add(VisitorModelDao.Properties.VisitorName.like("%" + VisitorName + "%"));
        }
        if (!TextUtils.isEmpty(StaffName)) {
            coditions.add(VisitorModelDao.Properties.StaffName.like("%" + StaffName + "%"));
        }
        if (!TextUtils.isEmpty(OrginazitionName)) {
            coditions.add(VisitorModelDao.Properties.OrginazitionName.like("%" + OrginazitionName + "%"));
        }
        if (state > 0) {
            coditions.add(VisitorModelDao.Properties.State.eq(state));
        }
        return coditions;
    }
  //
private Date getTime(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
        date = sdf.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

你可能感兴趣的:(greendao实现本地分页查询和模糊查询)