How to implement search in the ExpandableListView list?

 private String[] mGroupsArray = new String[] { "Зима", "Весна", "Лето", "Осень" }; private String[] mWinterMonthsArray = new String[] { "Декабрь", "Январь", "Февраль" }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("Locale Date"); Map<String, String> map; ArrayList<Map<String, String>> groupDataList = new ArrayList<>(); for (String group : mGroupsArray) { map = new HashMap<>(); map.put("groupName", group); groupDataList.add(map); } // список атрибутов групп для чтения String groupFrom[] = new String[] { "groupName" }; // список ID view-элементов, в которые будет помещены атрибуты групп int groupTo[] = new int[] { android.R.id.text1 }; // создаем общую коллекцию для коллекций элементов ArrayList<ArrayList<Map<String, String>>> сhildDataList = new ArrayList<>(); // в итоге получится сhildDataList = ArrayList<сhildDataItemList> // создаем коллекцию элементов для первой группы ArrayList<Map<String, String>> сhildDataItemList = new ArrayList<>(); // заполняем список атрибутов для каждого элемента for (String month : mWinterMonthsArray) { map = new HashMap<>(); map.put("monthName", month); // название месяца сhildDataItemList.add(map); } // добавляем в коллекцию коллекций сhildDataList.add(сhildDataItemList); // список атрибутов элементов для чтения String childFrom[] = new String[] { "monthName" }; // список ID view-элементов, в которые будет помещены атрибуты // элементов int childTo[] = new int[] { android.R.id.text1 }; SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupDataList, android.R.layout.simple_expandable_list_item_1, groupFrom, groupTo, сhildDataList, android.R.layout.simple_list_item_1, childFrom, childTo); ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expListView); expandableListView.setAdapter(adapter); } 

    2 answers 2

    How is the adapter implemented for the list? Apparently, the search should be performed on an array or container that was passed to ExpandableListView .

    UPDATE

    Get the adapter through the getExpandableListAdapter() method, and then, running through all groups and subgroups through the getGroup() and getChild() methods, check for your condition, depending on the implementation of the search mechanism.

    • developer.alexanderklimov.ru/android/views ... I did it here, help with the search - Dmitry
    • @ Dmitry Hmm, something this site does not open for me ...) although it usually works fine - Werder
    • help me with the code I have editText in which I need to enter letters by which to search and ExpandableListView whose code is higher, help me write the code completely - Dmitry
    • @ Dmitry after clicking on the search how should the list behave? leave only items matching the search? - Werder
    • Yes, after clicking on the search, only the elements corresponding to the search should remain - Dmitry

    You need to write your own adapter, which will implement the method of selecting only the necessary values ​​from the entire list by filtering string (filter by list) and in the implementation implement SearchManager (as well as SearchView in markup), which will pass to this filtering method in the adapter by what, actually, parameter (line) to sort. The filtering method in the adapter will form a new dataset and update the list ( notifyDataSetChanged() ), which will contain only sorted values ​​/
    In fact, you just need to generate new data for the list on the basis of some kind of user input, eliminating unnecessary data from the main data. The task is not complicated, but it is rather voluminous by code, so I suggest that you familiarize yourself with the full sample filter implementation