I enabled text filtering on my ListView in the expected way; adding android:textFilterEnabled=”true”
in resource definition and (as I'm using a SimpleCursorAdapter) setting a FilterQueryProvider that provides a filtered cursor like so:
public Cursor runQuery(CharSequence constraint) {
Cursor cur = mDba.fetchTrackers(mCurrentGroupId, constraint.toString());
开发者_Go百科startManagingCursor(cur);
return cur;
}
My question is this: once the user selects a list item, goes off to another activity, and returns to this one, how can I control the state of the filter previously applied?
What I'm seeing right now is that when I return to the activity, the cursor being used is the unfiltered one set on the filter at creation, but the filter text they typed is still shown (and typing keys causes the filter to be applied).
What I'd like to do is either clear the filter, or keep the filtered cursor that the activity was left with.
I had the same problem myself, although instead of setting a FilterQueryProvider
in the SimpleCursorAdapter
I used an overidden runQueryOnBackgroundThread(CharSequence constraint)
method when instantiating the SimpleCursorAdapter
.
E.g.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to) {
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint){
Cursor cur = mDba.fetchTrackers(mCurrentGroupId, constraint.toString());
startManagingCursor(cur);
return cur;
}
};
But if you want to clear the filter all you need to do is get theListView
and call itsclearTextFilter()
method. So if your Activity extendsListActivity
you could use the following:
ListView lv = getListView();
lv.clearTextFilter();
Add the above lines to the method where you re-load the data in the list, i.e. whenever yourListActivity
is shown.
Alternatively if you want to keep the filter active and re-apply it to theListActivity
when the user returns to it. You can get the current filter text, that has been entered already, from the ListActivity
as aCharSequence
and pass it to theSimpleCursorAdapter
's currentFilter
:
adapter.getFilter().filter(lv.getTextFilter());
Again add the above lines to the method where you re-load the data in the list and it will re-apply the existing filter.
精彩评论