Android_kalai
Android_kalai

Reputation: 423

How to use edit text as search box in expandable listview android?

In my application i am using expandable list view.Now i want to use search box for display the filtered expandable list view items.For this purpose i am using the following code

    search = (SearchView) findViewById(R.id.search);
    search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);

But this coding only supports above API 11.But i want to implement these feature in below API 11.

This is the way of using edit text as search view for the default list view adapter

  inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
    });

Upvotes: 3

Views: 6291

Answers (2)

Daniel Bo
Daniel Bo

Reputation: 2518

What i did is, why implemented a search for my Data myself.

i added a TextView to the Actionbar and i handle the input in my ListAdapter.

as you are targeting api below 11 you will either have to add ActionBarSherlock, or place the TextView elsewhere.

    EditText tv = new EditText(this);
    tv.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event.getAction() == KeyEvent.KEYCODE_ENTER) {
                yourAdapter.filterData(v.getText());
                return true;
            }
            return false;
        }
    });

this is how i would design a textView to handle a search. you will have to implement the search yourself, because my data is backed by an sqlite database so i just hand off the search to the sql database.

public void filterData(String query){

  query = query.toLowerCase();
  Log.v("MyListAdapter", String.valueOf(continentList.size()));
  continentList.clear();

  if(query.isEmpty()){
   continentList.addAll(originalList);
  }
  else {

   for(Continent continent: originalList){

    ArrayList<Country> countryList = continent.getCountryList();
    ArrayList<Country> newList = new ArrayList<Country>();
    for(Country country: countryList){
     if(country.getCode().toLowerCase().contains(query) ||
       country.getName().toLowerCase().contains(query)){
      newList.add(country);
     }
    }
    if(newList.size() > 0){
     Continent nContinent = new Continent(continent.getName(),newList);
     continentList.add(nContinent);
    }
   }
  }

  Log.v("MyListAdapter", String.valueOf(continentList.size()));
  notifyDataSetChanged();

 }

you would have to update the search method to fit your data.

Upvotes: 1

Dzmitry Lukoits
Dzmitry Lukoits

Reputation: 97

I think you need use actionBarCompat for backward compatibility. http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html

Upvotes: 0

Related Questions