Android: ExpandableListViews and Checkboxes

前端 未结 2 825
感动是毒
感动是毒 2021-01-05 21:01

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.

相关标签:
2条回答
  • 2021-01-05 21:35

    this is what i found in which a good guideline is given how we can make custom listview whether its a checkbox or any textview within listview

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

    Thanks..

    rakesh

    0 讨论(0)
  • 2021-01-05 21:57

    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

    • 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)

    methods in your BaseExpandableListAdapter extension.

    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;
        }
    }
    

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

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

    I hope this helps you

    0 讨论(0)
提交回复
热议问题