android可扩展列表视图从子onclick监听器获取组的视图

时间:2013-09-16 09:35:13

标签: android textview expandablelistview

我的应用程序中有一个可扩展的列表视图,每个组都有一个textview说T具有动态值,每个组中都有一个子项。我想引用那个孩子被点击的那个组的T textview对象,这样我可以根据点击的子项目相应地改变T的值。

我可以在groupClickListener的情况下轻松获取TextView对象,但是如果是childClickListener则无法获取...请参阅此处。

mExpandableListView.setOnGroupClickListener(new OnGroupClickListener(){

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // TODO Auto-generated method stub
            TextView moreTextView = (TextView)v.findViewById(R.id.group_more);  // this works fine

            }
            return false;
        }
    });

mExpandableListView.setOnChildClickListener(new OnChildClickListener(){

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            boolean isExpanded=false;
            if(groupStatus[groupPosition]==1)
            {
                isExpanded=true;
            }
            View v1 = parent.getExpandableListAdapter().getGroupView(groupPosition, isExpanded, v, parent);
            TextView moreTextView = (TextView)v1.findViewById(R.id.group_more);  // doesn,t work as v1 is null all the times
            return false;
        }
    });

2 个答案:

答案 0 :(得分:4)

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(
                getApplicationContext(),
                listDataHeader.get(groupPosition)
                        + " : "
                        + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                .show();
        return false;
    }
});

我希望它对你有用。

答案 1 :(得分:2)

使用"父母"上的getExpandableListAdapter()并相应地定义您的自定义类。 然后修改自定义对象中的值 - ExpandableListView将自动更新。 E.g:

@Override
public boolean onChildClick(ExpandableListView _parent, View _v,
        int _groupPosition, int _childPosition, long _id
) {
    // do required casts for your custom objects for groups and childs.
    // In my case, I'm using class "Group"
    Group group=(Group)parent.getExpandableListAdapter().getGroup(_groupPosition);
    // In my case, I'm using class "Child"
    Child item=(Child)optionsList.getExpandableListAdapter().getChild(_groupPosition, _childPosition);
    // In my custom Group class, I've defined a field that populates a different
    // TextView within the Group layout - so, getter/setter method for "selection" were defined.
    // The ExpandableListView will be updated automatically based on the adapter updates.
    group.setSelection(item.getName());
    return true;
}

不确定为什么会发生这种情况 - 似乎ExpandableAdapter正在跟踪模型本身的变化......我没有时间检查这个细节......但它确实有效。