我有一个EditText字段和两个ListView。 我想在EditText中输入数据,并在ListViews上显示两个不同的结果。 例如,第一个ListView显示以输入开头的单词,第二个ListView显示包含我的输入的单词。
为此,我有2个自定义ArrayAdapter。另外它们运行良好,但如果我尝试在TextWatcher中使用它们,它会开始出错。这有什么问题?实现我所描述的功能的最佳方法是什么?
提前致谢。
这是我的TextWatcher的代码:
TextWatcher watcher1;
watcher1 = new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
if (cs.length() == 0) {
//lv and lv2 are my ListViews
lv.setAdapter(null);
lv2.setAdapter(null);
} else {
//lv and lv2 are my ListViews
lv.setAdapter(adapterForSearch);
lv2.setAdapter(adapterForTurkishSearch);
adapterForSearch.getFilter().filter(cs);
adapterForTurkishSearch.getFilter().filter(cs);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
//inputSearch in my EditText object
inputSearch.addTextChangedListener(watcher1);
UPD: 这是我的适配器+过滤器代码。
public class MyAdapter<String> extends ArrayAdapter<String> {
protected Filter filter;
protected List<String> mObjects;
protected List<String> mOriginalValues;
protected List<String> tempList;
public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, (List<String>) objects);
filter = new MyFilter();
mObjects = (List<String>) objects;
tempList = new ArrayList<>();
//mOriginalValues = (List<String>) objects;
}
@Override
public Filter getFilter() {
return filter;
}
private class MyFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
{
mOriginalValues = new ArrayList<String>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<String> list;
{
list = new ArrayList<String>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
java.lang.String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values;
{
values = new ArrayList<String>(mOriginalValues);
}
final int count = values.size();
final ArrayList<String> newValues = new ArrayList<String>();
for (int i = 0; i < count; i++) {
final String value = values.get(i);
final java.lang.String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
/*final java.lang.String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}*/
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//mObjects = (List<String>) results.values;
mObjects.clear();
mObjects.addAll((List<String>) results.values);
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}