In my activity I have a ListView with a customadapter. The first item in that ListView is an EditText.
This EditText has a TextWatcher that causes the list to be filtered.
search.addTextChangedListener(textWatcher);
search.setOnFocusChangeListener(focusListener);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(getContext() instanceof ListActivity) {
ListAdapter adapter = ((ListActivity)getContext()).getListAdapter();
if(adapter instanceof TCListObjectAdapter) {
requestfocus = true;
((TCListObjectAdapter) adapter).getFilter().filter(s);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) { }
};
OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus && requestfocus){
search.post(new Runnable() {
@Override
public void run() {
search.requestFocus();
requestfocus = false;
}
});
}
}
};
The filtering of the list happens as expected. But sometimes after the text is changed, the first row of the listview becomes focussed: in this case the cell of the searchbox but not the EditText. If the user wants to continue filtering the list, he first has to select the EditText again.
I don’t want to turn off the option to focus cells in the listview for the visial effect.
Any ways to avoid losing focus of the EditText while filtering?
This fullfills these needs.