Android联系人过滤器无效 [英] Android contact filter not working
问题描述
我有一些示例代码,我想在其上添加搜索过滤器,在您键入任何字母进行过滤后,它不会显示已过滤的联系人或者它不会更新列表视图,任何人都可以对其进行说明,谢谢。
import ........
ContactAdapter objAdapter;
private ListView listView;
EditText inputSearch;
private List< contactbean> list = new ArrayList< contactbean>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView =(ListView)findViewById(R.id.list);
listView.setOnItemClickListener(this);
inputSearch =(EditText)findViewById(R.id.inputSearch);
listView =(ListView)findViewById(R.id.list);
Cursor phones = getContentResolver()。query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while(phones.moveToNext()){
String name = phones.getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactBean objContact = new ContactBean();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
list.add(objContact);
}
phones.close();
objAdapter = new ContactAdapter(MainActivity.this,R.layout.alluser_row,list);
listView.setAdapter(objAdapter);
if(null!= list&& list.size()!= 0){
Collections.sort(list,new Comparator< contactbean>(){
@Override
public int compare(ContactBean lhs,ContactBean rhs){
return lhs.getName()。compareTo(rhs.getName());
}
});
} else {
showToast(No Contact Found !!!);
}
inputSearch.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence cs,int arg1,int arg2,int arg3){
//当用户更改Text
MainActivity.this.objAdapter.getFilter()。filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0,int arg1,int arg2,int arg3){
// TODO自动生成的方法stub
}
@Override
public void afterTextChanged(Editable arg0){
// TODO自动生成的方法stub
}
});
}
您可能想在设置listView后尝试添加此行
inputSearch =(EditText)findViewById(R.id.inputSearch);
listView =(ListView)findViewById(R.id.list);
..
加上这个
// 启用对给定ListView内容的过滤
listView.setTextFilterEnabled(true);
还改变这个
@覆盖
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3){
// 当用户更改文本
MainActivity。此跨度> .objAdapter.getFilter()过滤器(CS)。
}
到
< pre lang =java> @ Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3){
// 当用户更改文本
objAdapter.getFilter()。filter(cs.toString());
}
我也在仿真器中找到了我必须按一封信然后返回给它工作,但我的模拟器目前用中文出于某种原因,不要问....
希望它有帮助
/ Darren
I got some sample code and i wanted to add search filter on it, after you type any letter to filter it won''t show the filtered contacts or it won''t update the listview, can anyone say something on it, thanks.
import ........ ContactAdapter objAdapter; private ListView listView; EditText inputSearch; private List<contactbean> list = new ArrayList<contactbean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list); listView.setOnItemClickListener(this); inputSearch = (EditText) findViewById(R.id.inputSearch); listView = (ListView) findViewById(R.id.list); Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (phones.moveToNext()) { String name = phones.getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ContactBean objContact = new ContactBean(); objContact.setName(name); objContact.setPhoneNo(phoneNumber); list.add(objContact); } phones.close(); objAdapter = new ContactAdapter(MainActivity.this, R.layout.alluser_row, list); listView.setAdapter(objAdapter); if (null != list && list.size() != 0) { Collections.sort(list, new Comparator<contactbean>() { @Override public int compare(ContactBean lhs, ContactBean rhs) { return lhs.getName().compareTo(rhs.getName()); } }); } else { showToast("No Contact Found!!!"); } inputSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text MainActivity.this.objAdapter.getFilter().filter(cs); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } }); }
You might want to try adding this line just after you setup your listView
inputSearch = (EditText) findViewById(R.id.inputSearch); listView = (ListView) findViewById(R.id.list); ..
add this
//enables filtering for the contents of the given ListView listView.setTextFilterEnabled(true);
Also Change this
@Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text MainActivity.this.objAdapter.getFilter().filter(cs); }
To
@Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text objAdapter.getFilter().filter(cs.toString()); }
I also found in the emulator I had to press a letter and return to get it to work but then my emulator is currently in Chinese for some reason, don''t ask....
Hope it helps
/Darren
这篇关于Android联系人过滤器无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!