Android GreenDao rawQuery 之 like 、 or、 = 拼接查詢條件

private void queryLocomotiveList() {

? ? ? ? List<Locomotive> locomotiveList = new ArrayList<>();

? ? ? ? String locomotiveModeName = tvEngineMode.getText().toString().trim();

? ? ? ? String locomotive = tvEngineNo.getText().toString().trim();

? ? ? ? try {

? ? ? ? ? ? //請求參數(shù)

? ? ? ? ? ? ArrayList<String> strParamLt = new ArrayList<String>();

? ? ? ? ? ? String queryString =

? ? ? ? ? ? ? ? ? ? "SELECT DISTINCT *? FROM " + LocomotiveDao.TABLENAME + " where 1==1 ";

? ? ? ? ? ? if (!TextUtils.isEmpty(locomotiveModeName)) {

? ? ? ? ? ? ? ? queryString = queryString + " and " + LocomotiveDao.Properties.TrainModelName.columnName + " =? ?";

? ? ? ? ? ? ? ? strParamLt.add(locomotiveModeName);

? ? ? ? ? ? }

? ? ? ? ? ? if (!TextUtils.isEmpty(locomotive)) {

? ? ? ? ? ? ? ? queryString = queryString + " and " + LocomotiveDao.Properties.TrainNo.columnName + " like '%" + locomotive + "%' ";

? ? ? ? ? ? }

? ? ? ? ? ? if (!TextUtils.isEmpty(healthy) || !TextUtils.isEmpty(attention) || !TextUtils.isEmpty(repair)){

? ? ? ? ? ? ? ? String healthyTypeStr = " and " + "(";

? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(healthy)) {

? ? ? ? ? ? ? ? ? ? healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " =? ?" + " or ";

? ? ? ? ? ? ? ? ? ? strParamLt.add(healthy);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(attention)) {

? ? ? ? ? ? ? ? ? ? healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " =? ?" + " or ";

? ? ? ? ? ? ? ? ? ? strParamLt.add(attention);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(repair)) {

? ? ? ? ? ? ? ? ? ? healthyTypeStr += LocomotiveDao.Properties.HealthAnalysisLevel.columnName + " =? ?";

? ? ? ? ? ? ? ? ? ? strParamLt.add(repair);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if (healthyTypeStr.endsWith("or ")){

? ? ? ? ? ? ? ? ? ? healthyTypeStr = healthyTypeStr.substring(0,healthyTypeStr.length() - 3);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? healthyTypeStr += ")";

? ? ? ? ? ? ? ? queryString = queryString + healthyTypeStr;

? ? ? ? ? ? }

? ? ? ? ? ? Log.e("queryString",queryString);

? ? ? ? ? ? Object[] objs = strParamLt.toArray();

? ? ? ? ? ? String[] strs = new String[objs.length];

? ? ? ? ? ? for (int i = 0; i < objs.length; i++) {

? ? ? ? ? ? ? ? strs[i] = objs[i].toString();

? ? ? ? ? ? }

? ? ? ? ? ? Log.e("strs",new Gson().toJson(strs));

? ? ? ? ? ? Cursor cursor = DaoManager.getInstance().getDaoSession().getDatabase().rawQuery(queryString, strs);

? ? ? ? ? ? int trainNoIndex = cursor.getColumnIndex(LocomotiveDao.Properties.TrainNo.columnName);

? ? ? ? ? ? int trainModeNameIndex = cursor.getColumnIndex(LocomotiveDao.Properties.TrainModelName.columnName);

? ? ? ? ? ? int healthIndex = cursor.getColumnIndex(LocomotiveDao.Properties.HealthAnalysisLevel.columnName);

? ? ? ? ? ? while (cursor.moveToNext()) {

? ? ? ? ? ? ? ? Locomotive bean = new Locomotive();

? ? ? ? ? ? ? ? bean.setTrainNo(cursor.getString(trainNoIndex));

? ? ? ? ? ? ? ? bean.setTrainModelName(cursor.getString(trainModeNameIndex));

? ? ? ? ? ? ? ? bean.setHealthAnalysisLevel(cursor.getString(healthIndex));

? ? ? ? ? ? ? ? locomotiveList.add(bean);

? ? ? ? ? ? }

? ? ? ? ? ? Log.e("locomotiveList",new Gson().toJson(locomotiveList));

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? mAdapter.setNewData(locomotiveList);

? ? }

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容