当用户在文本框中输入内容时,我想过滤我的列表。
但是尝试输入文本框时没有任何反应。
我看到了很多话题,但可以意识到哪里出了问题。
我知道这个问题被问了很多次,但我可以解决它。
这是我的代码:
public class Find extends ListActivity {
private EditText filterText = null;
DBHandler db=new DBHandler(this);
ListView myList;
String[] items = new String[10];
SimpleCursorAdapter adapter=null;
int test = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_search);
filterText = (EditText) findViewById(R.id.searchbox);
filterText.addTextChangedListener(filterTextWatcher);
Cursor cursor=db.getEvents();
startManagingCursor(cursor);
String[] from = {"_id","full_name"};
int[] to = { R.id.name_entry, R.id.number_entry};
adapter = new SimpleCursorAdapter(this, R.layout.activity_find, cursor, from, to);
setListAdapter(adapter);
我的文本观察方法:
private TextWatcher filterTextWatcher = new TextWatcher() {
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);
}
};