簡體   English   中英

使用嵌套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提取到其中嗎?

任何意見,將不勝感激!

我建議您將標頭數據和子數據保留在單獨的列表中。 我想做的是,我在單獨的引用中有子級和標頭數據。 所以我的構造函數看起來像這樣

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

    }

因此,在生孩子時,您可以這樣做。

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

好的,所以我解決了這個問題,這很簡單。 我創建了一個自己的自定義適配器,該適配器接收LinkedHashMap 我將鍵提取到用作組標題的List中。 然后,適配器提取嵌套的LinkedHashMap ,這些鍵用作子菜單項名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM