代码之家  ›  专栏  ›  技术社区  ›  Kevin Bradshaw

Android:可扩展列表视图和复选框

  •  5
  • Kevin Bradshaw  · 技术社区  · 14 年前

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

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

    我找到了这个教程, http://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.html ,看起来很完美。然而,当我编译它并开始玩它时,我意识到它非常麻烦。选中一个组中的复选框会导致另一个组中的随机框被选中或取消选中。,

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

    谢谢

    基夫

    2 回复  |  直到 14 年前
        1
  •  3
  •   rekaszeru    13 年前

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

    • public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    • public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

    你的方法 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 您应该在受影响的实例(在我的例子中是服务)上保存新状态。

    我希望这对你有帮助

        2
  •  0
  •   Rakesh Gondaliya    14 年前

    这是我发现的一个很好的指导方针,我们可以让自定义listview,无论它是一个复选框还是listview中的任何文本视图

    http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html

    谢谢。。