sqlite 에서 Like 검색 예제입니다.
실행 화면은 아래와 같습니다.
코드 작성
UI 처리 부분
public void setChangeListData(String searchKeyword) {
if(searchKeyword != null) {
if (searchKeyword.length() != 0) {
List<ItemMemo> temp = setLikeSearch(searchKeyword);
if(temp != null) {
setSearchData(temp);
return;
}
}
}
setLoadListData(oriList);
}
private void setSearchData(List<ItemMemo> list) {
lay_noData.setVisibility(View.GONE);
tv_noSearch.setVisibility(View.GONE);
if(list.size() == 0) {
rv.setVisibility(View.INVISIBLE);
tv_noSearch.setVisibility(View.VISIBLE);
} else {
rv.setVisibility(View.VISIBLE);
mAdapter.swap(list);
}
}
SQLite 쿼리 날리는 부분
/**
* Like 검색
*/
public List<ItemMemo> setLikeSearch(String keyword) {
List<ItemMemo> list = new ArrayList<>();
try {
beginTransaction();
Cursor c = select(TABLE_NAME, /**COLUMNS_NAME*/"contents", keyword);
if (c != null) {
Log.i("test","c : " + c + " , " + c.getCount());
int total = c.getCount();
if (total > 0) {
c.moveToFirst();
while (!c.isAfterLast()) {
/* 중략 */
list.add(new ItemMemo());
c.moveToNext();
}
}
c.close();
}
} catch (SQLiteException e) {
Log.e("test", e.toString());
} finally {
endTransaction();
}
return list;
}
public Cursor select(String tableName, String name, String Like) throws SQLiteException{
return getReadableDatabase().query(
tableName,
null,
name+ " LIKE ?",
new String[] {"%"+ Like+ "%" },
null,
null,
null,
null);
}
'Android' 카테고리의 다른 글
| [안드로이드] android google map cluster example (0) | 2017.04.18 |
|---|---|
| [안드로이드] Activity,Fragment 에서 Bundle 로 Object, ArrayList 넘기기 (1) | 2017.04.17 |
| [안드로이드] arraylist 초성 검색 예제 (0) | 2017.04.17 |
| [안드로이드] System.currentTimeMillis() 에서 몇분전, 몇시간전, 몇일전 등등 계산하기 (4) | 2017.04.16 |
| [안드로이드] 현재 fragment 불러오기 및 구분 (0) | 2017.04.16 |