I’m using the sample provided and have tweaked the code to my requirement. I’m learning Rx and not completely familiar with how it works. So, in my fragment I have
private static String LIST_QUERY = "SELECT * FROM " + TodoItem.TABLE;
And my onResume looks like:
@Override
public void onResume() {
super.onResume();
subscription = db.createQuery(TodoItem.TABLE, LIST_QUERY)
.mapToList(TodoItem.MAPPER)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(adapter);
}
The adapter showed the data in a listview and it works fine but now I have added a TextView on top of the list and want to filter results whenever the user enters text. I believe RxAndroid provides a debounce operator to make this efficient (and not query the DB instantly) but I’m unsure how I can combine this subscription code to listen to the textView changes and filter according to that text.
I already tried changing LIST_QUERY to "SELECT * FROM " + TodoItem.TABLE + " WHERE " + TodoItem.DESCRIPTION + " LIKE \"" + query + "\" LIMIT 5”;
and then in tried:
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence searchQuery, int i, int i1, int i2) {
query = searchQuery.toString();
adapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { //ignore }
@Override
public void afterTextChanged(Editable editable) { //ignore }
});
This resulted in the list being empty (I thought this was okay since the initial query is empty string) but when I typed some text the query didn’t update/filter the list at all.
I tried an alternate approach and added take(5) (since I want LIMIT 5) after mapToList and that worked and showed only 5 items. And then I added .filter(, typed new and let Android Studio generate the Func1 code and it looked like:
subscription = db.createQuery(ListItem.TABLE, LIST_QUERY)
.mapToList(Item.MAPPER)
.filter(new Func1<List<ListItem>, Boolean>() {
@Override
public Boolean call(List<ListItem> items) {
return null;
}
})
So problem is that it asks me to filter the whole list. I tried this:
public Boolean call(List<ListItem> items) {
for(ListItem i: items)
if(!i.description().startsWith(query))
items.remove(i);
return true;
}
But still the entire list shows and the changes in the text do not make the list change at all. I believe I missed something, that I have to subscribe to the EditText but I’m not sure how I would do this and combine it with this existing database subscribe code. Looking for solutions.