ExpandableListView 搜索过滤器

ExpandableListView Search Filter

我已经按照 Android ExpandableListView Search Filter Example 上的说明构建了一个可搜索的 ExpandableListView,但是在使用 onChildClick()

时,我正在努力 select 正确的 child

目前我正在使用下面的代码 select 并将信息发送到新的意图。但是,当应用搜索过滤器时,将发送该位置的原始 child 的信息,而不是过滤列表中 child 的信息。

    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;
        }
    });

我假设点击侦听器代码不在适配器 class 本身中,而是在 activity?在这种情况下,问题是您正在访问错误的数据集。当想要从适配器中检索数据时,请始终通过适配器方法。例如:

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

这适用于任何适配器。永远不要假设外部数据与内部适配器数据相同。