Android: ExpandableListViews and Checkboxes

限于喜欢 提交于 2020-01-21 05:01:06

问题


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.

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?

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

Thanks

Kev


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/3627096/android-expandablelistviews-and-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!