使用嵌套 LinkedHashMap 的 ExpandableListAdapter

ExpandableListAdapter using Nested LinkedHashMap

大家早上好,

我需要使用以下嵌套链接哈希图构建自定义适配器:LinkedHashMap<String, LinkedHashMap<String, Class<?>>>。我正在扩展 BaseExpandableListAdapter 并实现了所需的方法。我编写了以下代码以从 LinkedHashMap:

中获取组名
    private Context context;
    private LinkedHashMap<String, LinkedHashMap<String, Class<?>>> menuOptions;

    public customMenuAdapter(Context context, LinkedHashMap<String, LinkedHashMap<String, Class<?>>> menuOptions)
    {
        this.context = context;
        this.menuOptions = menuOptions;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return this.menuOptions.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public int getGroupCount()
    {
        return this.menuOptions.size();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        String gpsMenuGroupTitle = (String) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater gpsGroupInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = gpsGroupInflater.inflate(R.layout.gps_menu_list_header, null);
        }

        TextView gpsListHeaderText = (TextView) convertView.findViewById(R.id.gps_menu_list_header);
        gpsListHeaderText.setText(gpsMenuGroupTitle);

        return convertView;
    }

我必须从嵌套的 LinkedHashMap 中获取子项,但我不知道该怎么做。

getChild 方法中我只是 return this.menuOptions.get(groupPosition).get(childPosition); 吗?我应该创建一个字段并将嵌套的 LinkedHashMap 提取到其中吗?

如有任何建议,我们将不胜感激!

我建议您将 header 数据和 child 数据保存在单独的列表中。我喜欢做的是,我在单独的参考中有 child 和 header 数据。所以我的构造函数看起来像这样

public ExpandableListAdapter(Context _context, List<String> _headerDataList, HashMap<String, List<String>> _childDataList)
    {
        this._context = _context;
        this._headerDataList = _headerDataList;
        this._childDataList = _childDataList;

    }

所以在 get child 你这样做。

@Override
    public Object getChild(int groupPosition, int childPosition) {
        return _childDataList.get(_headerDataList.get(groupPosition)).get(childPosition);
    }

好的,所以我解决了这个问题,这很简单。我创建了一个自己的自定义适配器,它接收 LinkedHashMap。我将键提取到用作组标题的 List 中。然后适配器提取嵌套的 LinkedHashMap,键用作 child 菜单项名称。