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
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)
andpublic 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
精彩评论