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

前端 未结 2 1359
悲&欢浪女
悲&欢浪女 2021-01-06 20:16

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

相关标签:
2条回答
  • 2021-01-06 20:50

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

    0 讨论(0)
  • 2021-01-06 20:59

    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.

    0 讨论(0)
提交回复
热议问题