I have a textWatcher for my editText. The idea is:
I have two list views and they are both updated after every inserted in editText field character. Somehow I could't implement this using standart ArrayAdapter with filter method. So I had to make my own realization of search. At the beggining it seemed good, but as soon as I added to my data base more less enough data the interface started to freeze.
This is the call
inputSearch.addTextChangedListener(createAndReturnTextWatcher());
And this is the method that creates and returns textWatcher
private TextWatcher createAndReturnTextWatcher() {
final List<String> list = dbExtractor.getDataForSearchAdapters();
TextWatcher watcher1;
watcher1 = new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if (cs.length() == 0) {
lv.setAdapter(null);
lv2.setAdapter(null);
dc.clickedOnce = true;
dc.decrement();
dc.checkDimCounter();
} else {
setSecondListData(list, String.valueOf(cs));
setFirstListData(list, String.valueOf(cs));
if (dc.clickedOnce) {
dc.increment();
dc.checkDimCounter();
dc.clickedOnce = false;
}
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
};
return watcher1;
}
And this are methods that perform filtering
private void setSecondListData(List<String> inputData, String cs) {
List<String> result = turkishSearcher.performFiltering(inputData, cs);
ArrayAdapter turkAdapt = new ArrayAdapter(this, R.layout.dictionary_list_item, R.id.product_name,
result);
lv2.setAdapter(turkAdapt);
}
private void setFirstListData(List<String> inputData, String cs) {
List<String> result = normSearcher.performFiltering(inputData, cs);
ArrayAdapter turkAdapt = new ArrayAdapter(this, R.layout.dictionary_list_item, R.id.product_name,
result);
lv.setAdapter(turkAdapt);
}
And this is the class that filters data
public class TurkishSearcher extends Searcher{
@Override
int returnPosition() {
return 1;
}
@Override
String reverse(String couples) {
java.lang.String[] arr = couples.split(" — ");
return arr[1]+ " — " + arr[0];
}
@Override
String replaceTurkish(String words) {
if (checkWithRegExp(words)) {
return words.toLowerCase().replaceAll("ç", "c").replaceAll("ğ", "g").replaceAll("ı", "i").
replaceAll("ö", "o").replaceAll("ş", "s").replaceAll("ü", "u");
} else return words;
}
public static boolean checkWithRegExp(String word){
Pattern p = Pattern.compile("[öçğışü]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(word);
return m.find();
}
}
I clearly understand that the way I keep data is wrong and the search algorhytm isn't good as well. But my question is:
Is it possible to avoid freezes using Threads for search implementations. Can i puth both searchers to their own threads to somehow avoid interface freezes? If it's possible, pls tell me the best place in code where I can use threads.
Thank you in advance!