找到你要的答案

Q:ExpandableListView Search Filter

Q:可扩展的列表视图的搜索筛选器

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

I have constructed a searchable ExpandableListView following instructions at Android 可扩展的列表视图的搜索筛选器 Example but I'm struggling to select the correct child when using 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;
        }
    });
answer1: 回答1:

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.

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

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

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

android  expandablelistview  listadapter  expandablelistadapter