简体   繁体   English

如何在expandable listview android中使用编辑文本作为搜索框?

[英]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. 但是这种编码只支持上面的API 11.但我想在下面的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                          
        }
    });

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. 我在Actionbar中添加了一个TextView,我处理了ListAdapter中的输入。

as you are targeting api below 11 you will either have to add ActionBarSherlock, or place the TextView elsewhere. 当您将api定位到11以下时,您将需要添加ActionBarSherlock,或将TextView放在其他位置。

    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. 这是我如何设计textView来处理搜索。 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. 你必须自己实现搜索,因为我的数据由sqlite数据库支持,所以我只是将搜索交给sql数据库。

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. 您必须更新搜索方法以适合您的数据。

I think you need use actionBarCompat for backward compatibility. 我认为你需要使用actionBarCompat来实现向后兼容。 http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM