【问题标题】:Text Filter: strange behavior with SimpleCursorAdapter文本过滤器:SimpleCursorAdapter 的奇怪行为
【发布时间】:2010-10-25 20:25:06
【问题描述】:

我的列表视图上的过滤器有问题。 事实上,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 大同小异,不过我把 SimpleCursorAdapter 换成了 IndexAdapter

非常感谢您的帮助...

【问题讨论】:

标签: android listview text filter


【解决方案1】:

我认为您应该使用 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多