在为 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
您应该将新状态保存在受影响的实例(在我的例子中是服务)上。
我希望这可以帮助你