0

Good morning guys,

I need to build a custom adapter using the following nested linked hashmap: LinkedHashMap<String, LinkedHashMap<String, Class<?>>>. I am extending BaseExpandableListAdapter and have implemented the methods required. I have written the following code to get the group names from the 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;
    }

I have to get the child items from the nested LinkedHashMap and I have no idea how to do so.

In the getChild method do I simply return this.menuOptions.get(groupPosition).get(childPosition);? Should I create a field and extract the nested LinkedHashMap into it?

Any advice would be appreciated!

Owen Nel
  • 367
  • 3
  • 9
  • 21

2 Answers2

1

I would suggest you to keep header data and child data in separate list. What i like to do is, i have child and header data in separate reference. So my constructor look like this

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

    }

So in the get child you do this.

@Override
    public Object getChild(int groupPosition, int childPosition) {
        return _childDataList.get(_headerDataList.get(groupPosition)).get(childPosition);
    }
umerk44
  • 2,797
  • 4
  • 23
  • 43
  • This is what I did initially but my employer is insisting that we use the nested `LinkedHashMap`. We are planning to scale later and doing it that way makes scaling much easier in theory but makes creating the initial "superclasses" much more difficult and I am stuck on this. Obiviously putting the code in the the `LinkedHashMap` is not too difficult but working with it in the Adapter is then more complex – Owen Nel Sep 01 '15 at 08:25
  • So i think this will be nested LinkedHashMap. Then in your getchild() you will be doing something like this. String key = menuoption.get(groupPosition) menuOptions.get(key).get(key).getClass(); – umerk44 Sep 01 '15 at 11:24
-1

Ok, so I resolved this issue and it was quite simple. I created a my own custom adapter which receives the LinkedHashMap. I extract the keys into a List which is used as the Group Headings. The adapter then extracts the nested LinkedHashMap, the keys are used as the child menu item names.

Owen Nel
  • 367
  • 3
  • 9
  • 21