gpt4 book ai didi

java - Android:ExpandableListViews 和复选框

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:59:33 24 4
gpt4 key购买 nike

我最近一直在研究可扩展 ListView 。

我正在尝试获取一个 ListView ,其中有一个复选框作为 subview 的元素之一。

我找到了这个教程,http://mylifewithandroid.blogspot.com/2010/02/expandable-lists-and-check-boxes.html , 它看起来很完美。然而,当我编译它并开始使用它时,我意识到它有很多问题。选中一组中的一个框可能会导致另一组中的一个随机框选中或取消选中。

这是我能找到的唯一教程,它似乎会在很多应用程序中使用,所以我想知道,是否还有其他好的教程或资源可以处理这个问题?

或者更好的是,任何人都愿意展示他们自己的代码让这个工作......

谢谢

KV

最佳答案

我在为 android 开发 nagios 客户端时遇到了同样的问题,我发现,为两个

中的 convertView 参数分配新值至关重要
  • 公共(public) View getGroupView(int
    groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent)
  • 公共(public) View getChildView(int
    组位置,int childPosition,
    boolean 值 isLastChild, View
    convertView, ViewGroup 父级)

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 中,您应该将新状态保存在受影响的实例(在我的例子中是服务)上。

希望对你有帮助

关于java - Android:ExpandableListViews 和复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3627096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com