为了在Listview
中显示联系人姓名和手机号码,我创建了一个自定义SimpleCursorAdapter
来填充Listview
,这样可以正常使用。
我将EditText
控件添加到布局(位于Listview
之上)以进行过滤。
EditText
的代码 -
final EditText searchText = (EditText)findViewById(R.id.FilterTB);
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String text = searchText.getText().toString().toLowerCase(Locale.getDefault());
mySimpleCursorAdapter.getFilter().filter(text);
}
});
此代码无效。
如何以及以什么方式过滤客户SimpleCursorAdapter
??
修改
我的适配器类跟随 -
public class mySimpleCursorAdapter extends SimpleCursorAdapter {
HashMap selectedList;
Context cntx;
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
TextView tt = (TextView)view.findViewById(R.id.txtName);
String cid = cursor.getString(cursor.getColumnIndex( ContactsContract.Contacts._ID ));
int a = cursor.getPosition();
if(selectedList.containsKey(cid)){
tt.setTextColor(Color.RED);
} else{
tt.setTextColor(Color.WHITE);
}
}
private void saveSelectedList(){
SharedPreferences sharedPreferences = cntx.getSharedPreferences("myEvaluators", Context.MODE_PRIVATE);
String sl = "";
for (Object key : selectedList.keySet()){
//iterate over key
sl = sl + key.toString() + ";";
}
if(sl.length()>0) sl=sl.substring(0,sl.length()-1);
sharedPreferences.edit().putString("evaluators",sl).apply();
}
private void loadSelectedList(){
SharedPreferences sharedPreferences =
cntx.getSharedPreferences("myEvaluators", Context.MODE_PRIVATE);
String sl = sharedPreferences.getString("evaluators","");
String strArr[] = sl.split(";");
for (String s: strArr
) {
selectedList.put(s,s);
}
}
private void clearSelectedItems(){
for (Object s: selectedList.keySet()
) {
selectedList.remove(s);
}
saveSelectedList();
}
public void setClearSelectedItem(String _id, View v){
TextView tv = (TextView)v.findViewById(R.id.txtName);
if(selectedList.containsKey(_id)){
selectedList.remove(_id);
tv.setTextColor(Color.WHITE);
v.setTag(0);
} else{
selectedList.put(_id,_id);
tv.setTextColor(Color.RED);
v.setTag(null);
}
saveSelectedList();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
public mySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
selectedList = new HashMap();
cntx = context;
loadSelectedList();
}
}
答案 0 :(得分:0)
我建议使用另一个approch,即AutoCompleteTextView和SimpleCursorAdapter,以便加载联系人......
首先将AutoCompleteTextView放入Activity的布局xml文件中。
这是活动代码
public class MainActivity extendsActivity {
SimpleCursorAdapter mAdapter;
protected void onCreate(Bundle savedInstance) {
setContentView(R.layout.activity_main);
//assume that your AutoCompleteTextView element in your xml file is autoCompleteView
AutoCompleteTextView completeTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteView);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
new String[] { Contacts.DISPLAY_NAME },
new int[] {android.R.id.text1},
0);
completeTextView.setAdapter(mAdapter);
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence str) {
return getCursor(str);
} });
mAdapter.setCursorToStringConverter(new CursorToStringConverter() {
public CharSequence convertToString(Cursor cur) {
int index = cur.getColumnIndex(Contacts.DISPLAY_NAME);
return cur.getString(index);
}});
}
}
我们刚做了两件事。 首先调用setFilterQueryProvider。当我们在AutoCompleteTextView中更改内容时,每次执行此回调,FilterQueryProvider的runQuery函数。实际上这是重点。
其次,调用setCursorToStringConverter时,在我们选择item时使用它,它执行此函数将游标转换为String。
最后,一个缺失点,我们还没有共享getCursor(CharSequence str)函数。让我在下面分享。
public Cursor getCursor(CharSequence str) {
String select = "(" + Contacts.DISPLAY_NAME + " LIKE ? ";
String[] selectArgs = { "%" + str + "%"};
String[] contactsProjection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.LOOKUP_KEY, };
return getContentResolver().query(Contacts.CONTENT_URI, contactsProjection,
select, selectArgs, null);
}
将以上功能添加到您的活动类。
不要忘记在Android Manifest文件中添加READ_CONTACTS权限,以使此代码正常运行。
希望这有用。