2010-10-25 55 views
1

我有我的列表視圖上的過濾器的問題。 實際上,使用IndexAdapter可以很好地工作,但不能與SimpleCursorAdapter一起使用。文本過濾器:奇怪的行爲與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

感謝很多的幫助...

+0

剛剛發現了類似的問題,但我不明白的答案HTTP://stackoverflow.com/questions/2002607/android-how-to-text-filter-a-listview -based-on-a-simplecursoradapter – 2010-10-25 20:35:31

回答

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); 
} 
+1

這一個工程偉大!非常感謝你。 – Noufal 2014-01-02 18:45:46