安卓:ExpandableListViews和复选框 [英] Android: ExpandableListViews and Checkboxes

查看:134
本文介绍了安卓:ExpandableListViews和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在玩弄扩展列表视图。

I have being playing around with expandable list views recently.

我想获得一个列表视图,有一个复选框作为子视图的要素之一。

I am trying to get a list view that has a checkbox as one of the elements of the child view.

我发现这个教程中,<一个href="http://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.html">http://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.html,它似乎完美。然而,当我编译它,并开始使用它玩,我意识到它非常的马车。检查一组一箱可能会导致从另一组随机框选择或取消,

I found this tutorial, http://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.html, and it seemed perfect. However when I compiled it and started playing with it I realised its very buggy. Checking a box in one group can cause a random box from another group to check or uncheck.,

这是唯一的教程中,我可以找到关于这一点,这似乎是这将在许多应用程序中使用的东西,所以我想知道,有没有什么其他好的教程或资源在那里,这个处理?

This is the only tutorial I can find on this, It seems like a thing that would be used in a lot of apps, so I was wondering, is there any other good tutorial or resource out there that deals with this?

甚至更好,没有人会介意展现自己的code已经得到了这个工作...

Or even better, would anyone care to show their own code having gotten this to work...

感谢

千电子伏

推荐答案

我开发一个Nagios的客户端Android的时候有同样的问题,我发现,这是至关重要的,你在分配新值convertView参数均

I had the same problem when developing a nagios client to android, and i found, that is crucial that you assign new value to the convertView parameter in both the

  • 公开查看getGroupView(INT groupPosition,布尔isExpanded, 查看convertView,ViewGroup中父)
  • 公开查看getChildView(INT groupPosition,诠释childPosition, 布尔isLastChild,查看 convertView,ViewGroup中父)
  • public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) and
  • public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

BaseExpandableListAdapter 扩展方法。

否则,父/子渲染器会从缓存中建立起来的,他们不会告诉你正确的事情。

Otherwise the parent/child renderers will be built up from cache, and they won't show you the right things.

我需要的复选框用于显示用户是否需要任何警报东西是否出现问题与观看服务。

I need the checkbox for showing whether the user needs any kind of alert whether something goes wrong with the watched service.

下面是我实现这一目标的方式:

Here is my way of achieving this:

//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是包含复选框之一。

The childrow.xml among the layouts used is the one that contains the checkbox.

CheckedChanhedListener 你应该保存在受影响的实例的新状态(在我的情况,该服务)。

Inside the CheckedChanhedListener you should save the new state on the affected instance (in my case, the service).

我希望这可以帮助你。

这篇关于安卓:ExpandableListViews和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆