在ExpandableListView

时间:2018-12-24 16:11:53

标签: android filter hashmap expandablelistview

我想在可扩展列表视图中执行子项的搜索功能。我为子项有一个类型为(String,List String)的hashMap,其中List String包含所有子项。但是,我不知道如何从列表中过滤数据以在搜索查询中使用。

我已经尝试了stackoverflow帖子中的一些方法:Search a HashMap in an ArrayList of HashMapHow 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

        }

    }

2 个答案:

答案 0 :(得分:1)

尝试一下:

List<String> listDataHeader;
List<String> backupHeader;
private HashMap<String, List<String>> listHashMap;  // Backup lists.
private HashMap<String, List<String>> backupHashMap;

public void filterData(String query)
{
    if(backupHeader == null){     // Save original lists for the first time.
        backupHeader = new ArrayList<>();
        backupHeader.addAll(listDataHeader);
        backupHashMap = new HashMap<>();
        backupHashMap.putAll(listHashMap);
    }
    query = query.toLowerCase();
    listDataHeader.clear();
    listHashMap.clear();
    if(query.isEmpty())
    {
        listDataHeader.addAll(backupHeader);
        listHashMap.putAll(backupHashMap);
    }
    else // if user writes something
    {
        for(String header: backupHeader) {
            List<String> tmpList = new ArrayList<>();
            for(String item:backupHashMap.get(header)){
                if(item.toLowerCase().contains(query)) tmpList.add(item);
            }
            if(tmpList.size() > 0){
                listDataHeader.add(header);
                listHashMap.put(header, tmpList);
            }
        }
    }
    notifyDataSetChanged();
}

希望有帮助!

答案 1 :(得分:0)

如果要以可查询的方式寻求有效的搜索,则应考虑使用其他关系数据库类型,例如SQLite等。

如果您使用的是RxAndroid,则可以使用flatmap运算符,然后在结果流中进行搜索