J'ai eu le même problème lors de l'élaboration d'un client Nagios pour Android, et je trouve, qui est crucial que vous attribuez une nouvelle valeur au paramètre convertView tant dans le
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
et
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
méthodes dans votre BaseExpandableListAdapter
extension. Dans le cas contraire, les moteurs de rendu parent/enfant seront construits à partir du cache, et ils ne vous montreront pas les bonnes choses.
J'ai besoin de la case à cocher pour indiquer si l'utilisateur a besoin d'une sorte d'alerte si quelque chose ne va pas avec le service surveillé.
Voici ma façon d'y parvenir:
//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;
}
}
Le childrow.xml parmi les mises en page utilisée est celle qui contient la case à cocher.
À l'intérieur du CheckedChanhedListener
, vous devez enregistrer le nouvel état sur l'instance affectée (dans mon cas, le service).
J'espère que cela vous aide
Merci Rakesh, ressemble à un bon tutoriel, mais il semble traiter uniquement avec listviews, où que je suis plus intéressé par les ExpandableListViews .. –