【问题标题】:I cannot reset to my initial List when I delete search query on Search View in Android当我在 Android 的搜索视图上删除搜索查询时,我无法重置为我的初始列表
【发布时间】:2016-11-29 12:42:09
【问题描述】:

我是新手!用户,我正在尝试使用搜索视图过滤我的数据。过滤似乎有效,但是当我在搜索视图中删除文本以键入另一个文本时,我只有找到的项目,而不是我的初始列表。

更多分析我在适配器中使用以下内容:

public void filterData(String query){

    query = query.toLowerCase();

    if(query.isEmpty()){
        expListTitle .addAll(InitialList);

    }
    else {

        expListTitle.clear();
        for(String titles: NewexpListTitle)
        {
            if(titles.toLowerCase().contains(query))
            {
                expListTitle.add(titles);
            }
        }

    }

    notifyDataSetChanged();

}

并在适配器的构造函数中执行以下操作:

public CustomExpandableListAdapter(Context context, List<String> expListTitle, LinkedHashMap<String,ArrayList<Business>> expListDetail)
{
    this.context = context;
    this.expListTitle = expListTitle;
    this.expListDetail = expListDetail;

    this.NewexpListTitle= new ArrayList<String>();
    this.NewexpListTitle.addAll(expListTitle);

    this.InitialList = new ArrayList<String>();
    this.InitialList.addAll(expListTitle);
}

在我的课堂上,我给出了以下调用 FilterData 方法的内容

@Override
public boolean onQueryTextChange(String query) {
    listAdapter = new CustomExpandableListAdapter(getApplicationContext(), FinalListTitle, FinalMap);

    listAdapter.filterData(query);

    expandableListView.setAdapter(listAdapter);
    return false;
}

我有上面的 setAdapter 和初始化,因为数据来自使用 volley 的 JSON 对象

似乎当我清除 SearchView 时,列表 InitialList 也被清除,并且它的左侧与找到的元素一起被清除。 如何同时保留我的初始列表?

谢谢

【问题讨论】:

  • 参考this 链接。它会指导你
  • 为什么每次查询改变都要创建一个新的适配器?,只创建一个并在QueryTextChange上更新它的模型
  • @omar 因为数据来自使用 volley 的 JSON 对象

标签: android listview android-adapter listview-filter


【解决方案1】:
this below code solved my issue.

    @Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            lv_listname.clearTextFilter();
            listAdapter.getFilter().filter("");
        } else {
           listAdapter.getFilter().filter(newText);
          //  ad_names.notifyDataSetChanged();
          //  lv_listname.setFilterText(newText);
          //  searchview.dismissSuggestions();
        }
        return true;
    }

【讨论】:

    【解决方案2】:

    试试这个,

    @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            str=charSequence.toString();
            if(charSequence.length() >0){
                try {
    
                    vRow.clear();
                    for (RowItem r : vvRow){
                        ss= r.title.toLowerCase().toString();
                        if (ss.contains(str.toLowerCase())){
                            vRow.add(r);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }catch (Exception e){
                    Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }else{
                searchedt.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
                try {
                    vRow.clear();
                    for (RowItem r : vvRow){
                        vRow.add(r);
                    }
                    adapter.notifyDataSetChanged();
                }catch (Exception e){
                    Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      相关资源
      最近更新 更多