通讯录搜索功能,每输入一个字符,就会监听有所变化,这用到了TextWatcher监听EditText
this.search_content = (EditText) findViewById(R.id.search_content);
search_content.addTextChangedListener(new TextFilter());
TextFilter如下:
class TextFilter implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
searchContent = search_content.getText().toString();
// 从数据库中获得查询的联系人
contactDao = new ContactDao(getApplicationContext());
String Sql = "select * from contact_usr where username like '%"
+ searchContent + "%'";
String message = contactDao.execQuery(Sql);
search_userdata = getUserData(message);
// 为联系人创建listitem适配器
SimpleAdapter adapter = new MyAdapter(SearchUserActivity.this,
search_userdata, R.layout.userlist_item_layout,
new String[] { "user_photo", "user_name", "user_tel",
"user_store" }, new int[] { R.id.user_phote,
R.id.user_name, R.id.user_tel, R.id.store_user });
// 将listitem绑定到适配器上
search_user_list.setAdapter(adapter);
}
}