Java Android:ExpandableListView和复选框

Java Android:ExpandableListView和复选框,java,android,expandablelistview,Java,Android,Expandablelistview,我最近一直在玩可扩展列表视图 我正在尝试获取一个列表视图,其中有一个复选框作为子视图的元素之一 我找到了这个教程,它看起来很完美。然而,当我编译它并开始使用它时,我意识到它非常有缺陷。选中一组中的复选框可能会导致另一组中的随机框选中或取消选中 这是我能找到的关于这方面的唯一教程,它似乎会在很多应用程序中使用,所以我想知道,有没有其他好的教程或资源来处理这个问题 或者更好的是,是否有人愿意展示他们自己的代码,让它工作起来 谢谢 Kev这是我发现的,其中给出了一个很好的指导原则,我们可以如何定制li

我最近一直在玩可扩展列表视图

我正在尝试获取一个列表视图,其中有一个复选框作为子视图的元素之一

我找到了这个教程,它看起来很完美。然而,当我编译它并开始使用它时,我意识到它非常有缺陷。选中一组中的复选框可能会导致另一组中的随机框选中或取消选中

这是我能找到的关于这方面的唯一教程,它似乎会在很多应用程序中使用,所以我想知道,有没有其他好的教程或资源来处理这个问题

或者更好的是,是否有人愿意展示他们自己的代码,让它工作起来

谢谢


Kev

这是我发现的,其中给出了一个很好的指导原则,我们可以如何定制listview,无论它是复选框还是listview中的任何文本视图

谢谢


rakesh

我在为android开发nagios客户端时遇到了同样的问题,我发现,在两个

  • public View getGroupView(int)
    groupPosition,布尔值isExpanded,
    视图转换视图、视图组父视图)
  • 公共视图getChildView(int) groupPosition,int-childPosition, 布尔isLastChild,视图 convertView、ViewGroup父级)
BaseExpandableListAdapter
扩展中的方法

否则,父/子渲染器将从缓存中构建,并且它们不会显示正确的内容

我需要一个复选框,用于显示用户是否需要任何类型的警报,以确定监视的服务是否出了问题

以下是我实现这一目标的方法:

//hosts: the list of data used to build up the hierarchy shown by this adapter's parent list.
private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
    private LayoutInflater inflater;

    public MyExpandableListAdapter()
    {
        inflater = LayoutInflater.from(Binding.this);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        final Host host = hosts.get(groupPosition);
        final boolean needsLargeView = isExpanded && (host.getTitle() != null) && (host.getTitle().length() > 0);
        if (needsLargeView)
            convertView = inflater.inflate(R.layout.grouprow_expanded, parent, false);
        else
            convertView = inflater.inflate(R.layout.grouprow, parent, false);
        convertView.setBackgroundResource(host.getBackgroundResource(isExpanded));
        [...]
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        final Host host = hosts.get(groupPosition);
        final NagService service = host.getServices().get(childPosition);
        convertView = inflater.inflate(R.layout.childrow, parent, false);
        convertView.setBackgroundResource(host.getChildBackgroundResource());
        convertView.findViewById(R.id.servicename_status).setBackgroundResource(service.getStatusBackground());
        [...]
        CheckBox alertChb = (CheckBox) convertView.findViewById(R.id.alert);
        alertChb.setChecked(service.isNeedsAlert());
        alertChb.setOnCheckedChangeListener(new YourCheckChangedListener(service));
        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return hosts.get(groupPosition).getServices().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return hosts.get(groupPosition).getServices().size();
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return hosts.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        return hosts.size();
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public void notifyDataSetChanged()
    {
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isEmpty()
    {
        return ((hosts == null) || hosts.isEmpty());
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
}
使用的布局中的childrow.xml是包含复选框的布局

CheckedChanhedListener
中,您应该保存受影响实例(在我的例子中是服务)上的新状态


我希望这对您有所帮助

谢谢Rakesh,这看起来是一个不错的教程,但它似乎只处理ListView,因为我对ExpandableListView更感兴趣。。