【问题标题】:ExpandableListView Search FilterExpandableListView 搜索过滤器
【发布时间】:2015-08-14 14:37:54
【问题描述】:

我按照Android ExpandableListView Search Filter Example 的说明构建了一个可搜索的 ExpandableListView,但在使用 onChildClick() 时我很难选择正确的子项

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

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

【问题讨论】:

    标签: android expandablelistview listadapter expandablelistadapter


    【解决方案1】:

    我假设单击侦听器代码不在适配器类本身中,而是在活动中?在这种情况下,问题是您访问了错误的数据集。当想要从适配器检索数据时,总是通过适配器方法。例如:

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

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

    【讨论】:

    • 你的假设是正确的!使用适配器方法解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 2019-03-04
    • 2018-09-24
    • 2018-12-18
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多