文本过滤器:SimpleCursorAdapter的奇怪行为

时间:2010-10-25 20:25:07

标签: android listview text filter

我的listview上有一个过滤器问题。 事实上,使用IndexAdapter可以很好地工作,但不能使用SimpleCursorAdapter。

在以下示例中,如果isCursor == false,则过滤器工作得很好 但如果它是== true,则过滤器不起作用!

顺便说一句,适配器工作得很好。

if(isCursor){
    mCursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, stationsCursor, columns, to);
    FilterTextWatcherCursor filterTextWatcher = new FilterTextWatcherCursor(mCursorAdapter);
    filterText.addTextChangedListener(filterTextWatcher);
    this.setListAdapter(mCursorAdapter); 
}

else{
    mIndexAdapter = new MyIndexAdapter<String>(getApplicationContext(),
 R.layout.row_station_picker, elements);
    FilterTextWatcher filterTextWatcher = new FilterTextWatcher(mIndexAdapter);
    filterText.addTextChangedListener(filterTextWatcher);
    this.setListAdapter(mIndexAdapter);
    }

ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setFastScrollEnabled(true);

我真的不明白问题可能来自哪里。 有关信息,我的FilterTextWatcher是:

public class FilterTextWatcherCursor implements TextWatcher {

 private SimpleCursorAdapter adapter; 

 public FilterTextWatcherCursor(SimpleCursorAdapter adapter) {  
  this.adapter = adapter;
 }

 public void afterTextChanged(Editable s) {
 }

 public void beforeTextChanged(CharSequence s, int start, int count,
   int after) {
 }

 public void onTextChanged(CharSequence s, int start, int before,
   int count) {
  adapter.getFilter().filter(s);
 }

}

FilterTextWatcher非常相似,但我用IndexAdapter替换了SimpleCursorAdapter

非常感谢任何帮助...

1 个答案:

答案 0 :(得分:2)

我认为你应该使用FilterQueryProvider而不是TextWatcher。以下代码工作正常

class XXX extends Activty


class TextQuery implements FilterQueryProvider {
  @Override
  public Cursor runQuery(CharSequence arg0) {
    .... build a new select and provide a cursor
   return cursor;
    }
}


@Override
 protected void onCreate(Bundle savedInstanceState) {
 TextQuery textQuery = new TextQuery();
 simpleAdapter.setFilterQueryProvider(textQuery);
}