2

我的应用程序中有一个可扩展的列表视图,每个组都有一个 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;
        }
    });
4

2 回答 2

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

我希望它对你有用。

于 2013-09-16T10:00:45.177 回答
1

使用getExpandableListAdapter()“父级”并相应地定义您的自定义类。然后修改自定义对象中的值 - ExpandableListView 将自动更新。例如:

@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 似乎正在跟踪模型本身的变化......我没有时间检查这方面的细节......但它有效。

于 2014-01-31T18:00:10.570 回答