Search code examples
androidexpandablelistviewlistadapterexpandablelistadapter

ExpandableListView Search Filter


I have constructed a searchable ExpandableListView following instructions at Android ExpandableListView Search Filter Example but I'm struggling to select the correct child when using onChildClick()

At the moment I'm using the code below to select and send the information to a new intent. However, when a search filter is applied the information of the original child in that position is sent, and not of the child from the filtered list.

    myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            Object a = continentList.get(groupPosition).getCountryList().get(childPosition).getCode();
            Object b = continentList.get(groupPosition).getCountryList().get(childPosition).getName();
            Object c = continentList.get(groupPosition).getCountryList().get(childPosition).getPopulation();

            // Start new intent
            Intent intent = new Intent(SearchActivity.this, DisplayCountry.class);
            intent.putExtra("code", ""+ a);
            intent.putExtra("name", "" + c);
            intent.putExtra("population", "" + b);
            startActivity(intent);

            return true;
        }
    });

Solution

  • I'm assuming that click listener code is not in the adapter class itself, but in the activity? In which case the problem is that you are accessing the wrong set of data. When wanting to retrieve data from the adapter, always go through the adapters methods. Eg:

    mAdapter.getChild(groupPosition, childPosition).getCode();
    mAdapter.getChild(groupPosition, childPosition).getName();
    mAdapter.getChild(groupPosition, childPosition).getPopulation();
    

    This goes for any adapter. Never assume the external data is the same as the internal adapter data.