I have an EditText field and two ListViews. I want to input data in EditText and get two different results shown on ListViews. For example 1st ListView shows words that starts with input and second ListView shows words that contains my input.
For this I have 2 custom ArrayAdapters. Separately they work well, but if I try use them togethe in a TextWatcher it starts to bug a lot. What's an issue with this? And what is the best way to implement functionality I've described?
Thanks in advance.
This is the code of my 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: This is my Adapter + filter code.
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();
}
}
}
}