通过单击可展开列表视图中的子项来更改父项的值

时间:2013-05-05 21:44:19

标签: android

大家好我想通过单击可扩展列表中的子项来更改父级的值。我找了一个解决方案,但找不到任何东西。

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int              childPosition, long id){
            ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter();
            String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition);
            groupHeader(selectedItem);
            if(parent.isGroupExpanded(groupPosition)){
                parent.collapseGroup(groupPosition);
            }

            return true;
        }

    });

1 个答案:

答案 0 :(得分:0)

ExpandableListView依赖于适配器来获取其值。这些值来自某种数据结构,通常是ArrayList甚至是简单的数组。在onChildClick()中,传递对用于构建ExpandableListAdapter的数据结构的引用,并直接修改数据结构,然后调用notifyDataSetChanged()。

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
        ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter();
        String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition);
        groupHeader(selectedItem);
        if(parent.isGroupExpanded(groupPosition))
        {
            parent.collapseGroup(groupPosition);
        }
        // your logic here
        // expandableListArrayList.add() or expandableListArrayList.remove()
        // or whatever 
        itemAdapter.notifyDataSetChanged();

        return true;
    }
});

您可能必须扩展适配器才能创建所需的功能。你的问题有点模糊,因为我无法看到代码的整体结构,但这应该会让你有一个好的开始。