I have an EditText control on top of a ListView control in my layout. This layout is loaded into a Fragment derived class. I have also associated a TextWatcher interface with the EditText control so that each time text is entered into the EditText, the ListView filters the data based on the input text. The ListView's adapter implements Filterable behaviour by overriding GetFilter. The Fragment class that I am describing is part of a FragmentTabHost control which in turn loads 3 tabs at runtime.
Filtering the list based on what I enter in the EditText control works correctly. The issue I am facing is that when I click on some other Tab and then come back to my Tab which has this EditText with a TextWatcher, it is not clearing the previously entered text. I also notice that the filtered list is now empty. Once I manually remove the previous text from the EditText, the List re-appears with the old data.
Please let me know how to reset the filter and clear the text in the EditText control when I tab out of the current fragment. Basically I do not want the EditText control and the Filter to retain the old context each time I click on the Fragment. I have pasted sections of the code from the Fragment and List Adapter.
Thanks Balaji
public class Favourites extends Fragment implements LoaderCallbacks<Cursor> {
ListView listView1;
ArrayList<FavouriteSearchResults> searchResults;
FavouritesBaseAdapter customAdapter;
private ProgressBar progressBar;
private EditText editText;
final int MENU_MAKE_CALL = 100;
final int MENU_SEND_MESSAGE = 101;
final int MENU_SEND_DIAL = 102;
final int MENU_COPY_CLIP = 103;
final int MENU_SEND_NUMBER = 104;
final int MENU_VIEW_CONTACT = 105;
private static final int LIST_ID = 1004;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setHasOptionsMenu(true); //Call this so that onCreateOptionsMenu is called
}
private class MyFocusChangeListener implements OnFocusChangeListener {
Context mContext;
public MyFocusChangeListener(Context context) {
mContext = context;
}
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.txtSearch && !hasFocus) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.favourites, container, false);
editText = (EditText) v.findViewById(R.id.txtSearch);
OnFocusChangeListener ofcListener = new MyFocusChangeListener(getActivity());
editText.setOnFocusChangeListener(ofcListener);
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
customAdapter.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
if(charSequence == null || charSequence.length() == 0) {
results.values = searchArrayList; //Return original list if search is cleared
results.count = searchArrayList.size();
}
else {
ArrayList<FavouriteSearchResults> tempResults = new ArrayList<FavouriteSearchResults>();
for(int i = 0;i < searchArrayList.size();i++) {
FavouriteSearchResults favResults = searchArrayList.get(i);
String sContactName = favResults.GetName();
String sId = favResults.GetId();
String searchChar = charSequence.toString();
if (sContactName.toLowerCase().contains(searchChar.toLowerCase())) {
FavouriteSearchResults newFavResults = new FavouriteSearchResults(sContactName);
newFavResults.SetId(sId);
tempResults.add(newFavResults);
}
}
results.values = tempResults;
results.count = tempResults.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
//Set the filtered list into our copy list
searchFilteredArrayList = (ArrayList<FavouriteSearchResults>)filterResults.values;
notifyDataSetChanged();
}
};