【问题标题】:How to use edit text as search box in expandable listview android?如何在可扩展列表视图 android 中使用编辑文本作为搜索框?
【发布时间】:2013-10-14 08:32:24
【问题描述】:

在我的应用程序中,我正在使用可扩展列表视图。现在我想使用搜索框来显示过滤后的可扩展列表视图项目。为此,我使用以下代码

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

但是这种编码只支持 API 11 以上,但我想在 API 11 以下实现这些功能。

这是使用编辑文本作为默认列表视图适配器的搜索视图的方式

  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                          
        }
    });

【问题讨论】:

  • 请给我一些关于如何使用编辑文本在自定义展开列表视图中搜索的建议

标签: android expandablelistview


【解决方案1】:

我所做的是,为什么要自己实现对我的数据的搜索。

我在 Actionbar 中添加了一个 TextView,并在我的 ListAdapter 中处理输入。

由于您的目标是 11 以下的 api,您要么必须添加 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;
        }
    });

这就是我将如何设计一个 textView 来处理搜索。您必须自己实现搜索,因为我的数据由 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();

 }

您必须更新搜索方法以适应您的数据。

【讨论】:

  • 添加了一些可用作 SearchView 的 TextView 示例代码。
  • 不。这不是我的解决方案。因为我使用的是自定义可扩展列表视图适配器。
  • 是的,您必须在适配器中搜索数据。您的问题是您将无法在 api 11 及更低版本中使用 SearchView。所以你需要使用别的东西。例如,我刚刚注意到的 TextView 应该是一个 EditText =D 并且您需要添加一个能够在您的数据中搜索的方法。
  • 是的,你也可以这样做。唯一的区别是我的方法没有实时搜索。我等待用户按回车键开始搜索。每次用户更改输入时,您都在搜索。
  • 是的..但是我发布的编码不适用于我的自定义可扩展列表视图
【解决方案2】:

我认为您需要使用 actionBarCompat 来实现向后兼容性。 http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html

【讨论】:

  • 尝试使用 SearchViewCompat。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 2017-05-19
相关资源
最近更新 更多