【问题标题】:android expandable list view get view of group from child onclick listenerandroid可扩展列表视图从子onclick侦听器获取组视图
【发布时间】:2013-09-16 09:35:12
【问题描述】:

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

【问题讨论】:

  • 请展示你做了什么

标签: android textview expandablelistview


【解决方案1】:
// 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;
    }
});

希望对你有用。

【讨论】:

  • 我将如何从中获取对 TextView 对象的引用,以及这里的 listDataChild 是什么。
【解决方案2】:

在“父级”上使用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 正在跟踪模型本身的更改...我没有时间检查这方面的详细信息.. 但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多