我有一个包含名称和 AutoCompleteTextView 的列表。我需要 AutoCompleteTextView 来按用户输入的字符串过滤名称,但我不知道我做错了什么,因为 AutoCompleteTextView 不起作用。你能帮助我吗?
这是我的代码:
cursor = socioData.getAllSocios();
adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_dropdown_item_1line,
cursor,
new String[]{Socio.C_NOME},
new int[]{android.R.id.text1}
);
filterText = (AutoCompleteTextView)findViewById(R.id.search);
filterText.setThreshold(1);
filterText.setAdapter(adapter);
adapter.setCursorToStringConverter(new CursorToStringConverter() {
@Override
public String convertToString(Cursor cursor) {
//return cursor.getString(1);
final int colIndex = cursor.getColumnIndexOrThrow(Socio.C_NOME);
return cursor.getString(colIndex);
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint){
return getContentResolver().query(Data.CONTENT_URI,
Socio.ALL_COLUMNS,
Socio.C_NOME + " like '%"+constraint+"%'",
null,
Socio.C_NOME +" ASC");
}
});
filterListener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
if ("".equals(s))
adapter.getFilter().filter(null);
else
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void afterTextChanged(Editable arg0) {}
};