开发者

Returning to a ListView with a text filter

开发者 https://www.devze.com 2022-12-19 22:05 出处:网络
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 th

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 theListViewand call itsclearTextFilter()method. So if your Activity extendsListActivityyou 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 yourListActivityis shown.

Alternatively if you want to keep the filter active and re-apply it to theListActivitywhen the user returns to it. You can get the current filter text, that has been entered already, from the ListActivityas aCharSequenceand 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消