【问题标题】:Accessing groupview's TextView from getChildView() of ExpandableListView从 ExpandableListView 的 getChildView() 访问 groupview 的 TextView
【发布时间】:2015-10-14 16:29:45
【问题描述】:

我正在尝试访问 GroupView 的文本视图,它显示 ChildView 中选定复选框的计数。

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

这是我的代码 sn-p -

@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, View convertView, final ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_expandable_list_child,
                parent, 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;
}

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    首先,以简单列表视图为例,而不是可扩展列表视图。 ListView 是包含一堆 Items 的容器。
    这些项目每个都有子视图,这些子视图由组成 ListView 中的一行的各个元素组成。即不同的文本视图等。
    适配器的 getView() 对数据集进行操作,然后在列表中创建项目。因此,如果您更改用于创建适配器的数据集,并调用 notifyDatasetChanged(),那么它会更新列表视图。

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

    假设您使用 mList(它是 Object 类型的 ArrayList)来创建适配器

    // ArrayList<Object> mList;
    // You created mAdapter from this mList
    // now mList is data set for this adapter
    

    所以在 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 本身初始化 count,我建议您将 count 存储在 Object 本身中,并从那里初始化 textView 的值。
    所以在 getGroupView()

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

    【讨论】:

    • 答案简洁明了!
    猜你喜欢
    • 2014-04-30
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多