Created
May 15, 2016 02:20
-
-
Save bowyer-app/2f8a51665606b496d3b5bdca32e4c7ef to your computer and use it in GitHub Desktop.
Androidアプリで検索機能実装にExpandableLayoutを使ったらとても使いやすくなった ref: http://qiita.com/bowyer-app/items/6db6daceeccb38a3a1eb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public QueryObservable createQuery(@NonNull final String table, @NonNull String sql, | |
@NonNull String... args) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String listId = String.valueOf(getListId()); | |
subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId) | |
.mapToList(TodoItem.MAPPER) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(adapter)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String LIST_QUERY = "SELECT * FROM " | |
+ TodoItem.TABLE | |
+ " WHERE " | |
+ TodoItem.LIST_ID | |
+ " = ? ORDER BY " | |
+ TodoItem.COMPLETE | |
+ " ASC"; | |
@Inject BriteDatabase db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void doSearch() { | |
String name = mSearchQueryText.getText().toString(); | |
Sex sex = getCheckedSex(mSexGroup.getCheckedRadioButtonId()); | |
Rank rank = Rank.of(mRankText.getText().toString()); | |
PlayerDao.PlayerQuery playerQuery = | |
new PlayerDao.SQLBuilder().name(name).sex(sex).rank(rank).build(); | |
mSubscription = (PlayerDao.getPlayerByQuery(mDb, playerQuery) | |
.mapToList(Player.MAPPER) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(mAdapter)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final class SQLBuilder { | |
private String sql = "SELECT * FROM " + Player.TABLE + " WHERE "; | |
private final List<String> values = new ArrayList<>(); | |
private String getAndSql() { | |
if (!values.isEmpty()) { | |
sql = sql + " and "; | |
} | |
return sql; | |
} | |
private String getOrSql() { | |
if (!values.isEmpty()) { | |
sql = sql + " or "; | |
} | |
return sql; | |
} | |
public SQLBuilder name(String name) { | |
if (TextUtils.isEmpty(name)) { | |
return this; | |
} | |
//全部マッチさせるため | |
sql = getAndSql() + Player.LAST_NAME + " like ? "; | |
values.add("%" + name + "%"); | |
sql = getOrSql() + Player.FIRST_NAME + " like ? "; | |
values.add("%" + name + "%"); | |
sql = getOrSql() + Player.LAST_NAME_PHONETIC + " like ? "; | |
values.add("%" + name + "%"); | |
sql = getOrSql() + Player.FIRST_NAME_PHONETIC + " like ? "; | |
values.add("%" + name + "%"); | |
return this; | |
} | |
public SQLBuilder sex(Sex sex) { | |
if (sex == Sex.ALL) { | |
return this; | |
} | |
sql = getAndSql() + Player.SEX + " = ? "; | |
values.add(String.valueOf(sex.getSex())); | |
return this; | |
} | |
public SQLBuilder rank(Rank rank) { | |
if (rank == Rank.ALL) { | |
return this; | |
} | |
sql = getAndSql() + Player.RANK + " = ? "; | |
values.add(rank.getRank()); | |
return this; | |
} | |
public SQLBuilder id(long playerId) { | |
sql = getAndSql() + Player.ID + " = ? "; | |
values.add(String.valueOf(playerId)); | |
return this; | |
} | |
public PlayerQuery build() { | |
if (values.isEmpty()) { | |
sql = sql.replace("WHERE", ""); | |
} | |
return new PlayerQuery(sql, values.toArray(new String[values.size()])); | |
} | |
} | |
public static class PlayerQuery { | |
public final String sql; | |
public final String[] values; | |
public PlayerQuery(String sql, String[] values) { | |
this.sql = sql + " ORDER BY " + Player.ID + " DESC, " + Player.LAST_NAME + " ASC"; | |
this.values = values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment