android – 从ExpandableListView的getChildView()访问groupview的TextView

前端之家收集整理的这篇文章主要介绍了android – 从ExpandableListView的getChildView()访问groupview的TextView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试访问GroupView的textview,它显示ChildView中所选复选框的计数.

enter image description here

例如 – North是GroupView,下面带有复选框的列表是ChildView.我想每次点击复选框更新计数(18).我在复选框上应用了OnClickListner.我有自定义ExpanadableListViewAdapter扩展BaseExpandableListAdapter.

这是我的代码片段 –

@Override
public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_expandable_list_group,parent,false);

        groupViewHolder = new GroupViewHolder();

        groupViewHolder.GroupName = (TextView) convertView.findViewById(R.id.group_name);
        groupViewHolder.GroupCount = (TextView) convertView.findViewById(R.id.group_count);
        groupViewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.right_arrow);

        convertView.setTag(groupViewHolder);
    }else{
        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }
    groupViewHolder.GroupName.setText(((OutletListData) getGroup(groupPosition)).getName());
    groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getOutletDatas().size());

    return convertView;
}

@Override
public View getChildView(final int groupPosition,final int childPosition,boolean isLastChild,final ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_expandable_list_child,false);

        childViewHolder = new ChildViewHolder();

        childViewHolder.childTextView = (TextView) convertView.findViewById(R.id.text1);
        childViewHolder.childCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox);

        convertView.setTag(childViewHolder);
    }else{
        childViewHolder = (ChildViewHolder) convertView.getTag();
    }

    childViewHolder.childTextView.setText(((OutletData) getChild(groupPosition,childPosition)).getDealerName());
    childViewHolder.childCheckBox.setChecked(((OutletData) getChild(groupPosition,childPosition)).getSelected() == "1");
    childViewHolder.childCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isChecked = ((CheckBox) v).isChecked();
            ApplicationController.getEventBus().post(((OutletData) getChild(groupPosition,childPosition)).getOutletID());
            if (isChecked) {
                ((OutletData) getChild(groupPosition,childPosition)).setSelected("1");
            } else {
                ((OutletData) getChild(groupPosition,childPosition)).setSelected("0");
            }
        }
    });
    return convertView;
}
最佳答案
首先,以简单列表视图为例,而不是可扩展列表视图. ListView是容纳一堆Items的容器.
每个项目都有子视图,这些视图由构成ListView中一行的各个元素组成.即不同的文本视图等.
适配器的getView()对数据集进行操作,然后在列表中创建项目.因此,如果更改用于创建适配器的数据集,并调用notifyDatasetChanged(),则会更新列表视图.

现在在你的情况下,
您可能正在使用ArrayList来表示像“NORTH”这样的对象.因此,如果将count值存储在此对象中,则计数的更新将很容易.
只需使用groupPosition访问列表中的数据并进行更新即可.并调用notifyDatasetChanged.

假设您使用了mList类型的ArrayList来创建适配器

// ArrayList

所以在getChildView()中你写道:

@Override
    public void onClick(View v) {
        boolean isChecked = ((CheckBox) v).isChecked();
        ApplicationController.getEventBus().post(((OutletData) getChild(groupPosition,childPosition)).getOutletID());
        if (isChecked) {
            // your code 
            int count = ((OutletListData) getGroup(groupPosition)).getCount();
            count++;
            ((OutletListData) getGroup(groupPosition)).setCount(count);
            notifyDataSetChanged();
        } else {
            // your code;
            int count = ((OutletListData) getGroup(groupPosition)).getCount();
            count--;
            ((OutletListData) getGroup(groupPosition)).setCount(count);
            notifyDataSetChanged();

        }
    }

现在,这种方法的唯一限制是count应该是Object的一部分.因此,如果您是第一次从其他地方初始化计数而不是从Object本身,我建议您将计数存储在Object本身,并从那里初始化textView的值.
所以在getGroupView()中

groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getCount());

猜你在找的Android相关文章