【发布时间】:2019-05-23 17:51:42
【问题描述】:
我想在可扩展列表视图中执行子项的搜索功能。对于子项,我有一个类型为 (String, List String) 的 hashMap,其中 List String 包含所有子项。但是,我不知道如何过滤列表中的数据以用于我的搜索查询。
我尝试了一些来自 stackoverflow 帖子的方法:Search a HashMap in an ArrayList of HashMap, How to use edit text as search box in expandable listview android? 和其他几个,但无济于事。
MainActivity.java
List<String> listP = new ArrayList<>();
HashMap<String, List<String>> listC = new HashMap<>();
ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(this, listP, listC, this);
listP.add("Movie");
listP.add("Building");
listP.add("Car");
List <String> movie = new ArrayList<>();
movie.add("horror");
movie.add("action");
List <String> building = new ArrayList<>();
building.add("bank");
building.add("hotel");
List <String> car = new ArrayList<>();
car.add("BMW");
car.add("Mercedes");
listC.put(listP.get(0),movie);
listC.put(listP.get(1),building);
listC.put(listP.get(2),car);
ExpandableListAdapter.java
List<String> listDataHeader;
private HashMap<String, List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap, Activity activity) { this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
this.activity = activity;
}
public void filterData(String query)
{
query = query.toLowerCase();
listDataHeader.clear();
if(query.isEmpty())
{
listDataHeader.clear();
}
else // if user writes something
{
// I don't know how to get the values from the child items to compare it with query
}
}
【问题讨论】:
标签: android filter hashmap expandablelistview