在expandableListView的第三层中填充不同的数组 [英] Populate different arrays in third layer of expandableListView

查看:85
本文介绍了在expandableListView的第三层中填充不同的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还比较陌生.我想这是一个非常简单的逻辑,但请忍受我.我正在研究三个级别的expandableListView.我找到了一个非常适合我的教程.我的问题是我需要用不同的数组填充第三级.

I am relatively new to programming. I guess this is a very simple logic but please bear with me. I am working on a three level expandableListView. I found a tutorial that works perfectly for me. My problem is that i need to populate the third level with different arrays.

这是

  public class ParentLevelAdapter extends BaseExpandableListAdapter {

    private final Context mContext;
    private final List<String> mListDataHeader;
    private final Map<String, List<String>> mListData_SecondLevel_Map;
    private final Map<String, List<String>> mListData_ThirdLevel_Map;

    public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) {
        this.mContext = mContext;
        this.mListDataHeader = new ArrayList<>();
        this.mListDataHeader.addAll(mListDataHeader);
        // SECOND LEVEL
        String[] mItemHeaders = new String[0];
        mListData_SecondLevel_Map = new HashMap<>();
        int parentCount = mListDataHeader.size();
        for (int i = 0; i < parentCount; i++) {
            String content = mListDataHeader.get(i);
            Log.e("Content is: ", content );
            switch (content) {
                case "Level 1.1":
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
                    break;
                case "Level 1.2":
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
                    break;
                default:
                    mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_two);
            }
            mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
        }
        // THIRD LEVEL
        String[] mItemChildOfChild;
        List<String> listChild;
        String contento = Arrays.toString(mItemHeaders);
        Log.e("next content: ", contento);
        mListData_ThirdLevel_Map = new HashMap<>();
        for (Object o : mListData_SecondLevel_Map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            Object object = entry.getValue();
            if (object instanceof List) {
                List<String> stringList = new ArrayList<>();
                Collections.addAll(stringList, (String[]) ((List) object).toArray());
                for (int i = 0; i < stringList.size(); i++) {
                    mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three_samsung);
                    listChild = Arrays.asList(mItemChildOfChild);
                    mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
                }
            }
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final CustomExpandableListView secondLevelExpListView = new CustomExpandableListView(this.mContext);
        String parentNode = (String) getGroup(groupPosition);
        secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
        secondLevelExpListView.setGroupIndicator(null);
        secondLevelExpListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousGroup = -1;
            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousGroup)
                    secondLevelExpListView.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });
        return secondLevelExpListView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_second, parent, false);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

这是我的array.xml

This is my array.xml

<?xml version="1.0" encoding="utf-8"?>

<string-array name="items_array_expandable_level_one">
    <item>Mobile</item>
    <item>Laptops</item>
     <item>VR</item>
    <item>Misc</item>
</string-array>
<string-array name="items_array_expandable_level_one_one_child">
    <item>Samsung</item>
    <item>iPhone</item>
    <item>Nexus</item>
</string-array>
<string-array name="items_array_expandable_level_one_two_child">
    <item>Dell</item>
    <item>HP</item>
    <item>ACER</item>
</string-array>
<string-array name="items_array_expandable_level_two">
    <item>Mobile</item>
    <item>Laptops</item>
     <item>VR</item>
    <item>Misc</item>
</string-array>
<string-array name="items_array_expandable_level_three_samsung">
    <item>Galaxy S4</item>
    <item>Galaxy S5</item>
    <item>Galaxy S6</item>
    <item>Galaxy S7</item>
</string-array>
 <string-array name="items_array_expandable_level_three_iPhone">
    <item>iPhone 4s</item>
    <item>iPhone 5</item>
    <item>iPhone SE</item>
    <item>iPhone 7</item>
</string-array> .... and so on

我很确定我必须在表示第三级的地方编写我的代码.第二层我能够相应地填充.请向我提供有关如何在上述代码中填充不同数组的逻辑.我希望我对我的问题很清楚.预先感谢

I am pretty sure i have to write my code where it says the third level. the second layer i am being able to populate accordingly. Please provide me with the logic as to how i can populate different array in the above code. I hope i am clear with my problem. Thanks in advance

推荐答案

当Op将此Github示例作为其基础时:

When the Op is using this Github example as his bases: https://github.com/ngocchung/ThreeLevelExpListView and also uses the included SecondlevelAdapter, the added code section should solves his problem.

在第三级中,需要添加与第二级中的语句一致的switch语句.其中包括String定义和大小写,取决于XMl的Menu项,并根据大小写对mItemChildOfChild进行定义.

In the Third Level, there needs to be added an the switch statement coherent to the one on the Second Level. This includes the String definition and the case changing depending on Menu items of XMl and defing the mItemChildOfChild depending on the case.

public class ParentLevelAdapter extends BaseExpandableListAdapter {

private final Context mContext;
private final List<String> mListDataHeader;
private final Map<String, List<String>> mListData_SecondLevel_Map;
private final Map<String, List<String>> mListData_ThirdLevel_Map;

public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) {
this.mContext = mContext;
this.mListDataHeader = new ArrayList<>();
this.mListDataHeader.addAll(mListDataHeader);
// SECOND LEVEL
String[] mItemHeaders = new String[0];
mListData_SecondLevel_Map = new HashMap<>();
int parentCount = mListDataHeader.size();
for (int i = 0; i < parentCount; i++) {
    String content = mListDataHeader.get(i);
    Log.e("Content is: ", content );
    switch (content) {
        case "Level 1.1":
            mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
            break;
        case "Level 1.2":
            mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
            break;
        default:
            mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_two);
    }
    mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
}
// THIRD LEVEL
String[] mItemChildOfChild;
List<String> listChild;
String contento = Arrays.toString(mItemHeaders);
Log.e("next content: ", contento);
mListData_ThirdLevel_Map = new HashMap<>();
for (Object o : mListData_SecondLevel_Map.entrySet()) {
    Map.Entry entry = (Map.Entry) o;
    Object object = entry.getValue();
    if (object instanceof List) {
        List<String> stringList = new ArrayList<>();
        Collections.addAll(stringList, (String[]) ((List) object).toArray());
        for (int i = 0; i < stringList.size(); i++) {
 //newly inserted Code

String content = stringList.get(i);
switch (content) {
case "Samsung":
mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three_samsung);
break;
case "iPhone": //though I would change this to Apple and aswell in the Itemlist ;) :D
mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three_iPhone);
break;
default:
mItemChildOfChild = mContext.getResources().getStringArray(R.array.empty);}

//end of new Code

            listChild = Arrays.asList(mItemChildOfChild);
            mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
        }
    }
}
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

    @Override
    public View getChildView(int groupPosition, int childPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {
final CustomExpandableListView secondLevelExpListView = new 
CustomExpandableListView(this.mContext);
String parentNode = (String) getGroup(groupPosition);
secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, 
mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
secondLevelExpListView.setGroupIndicator(null);
secondLevelExpListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    int previousGroup = -1;
    @Override
    public void onGroupExpand(int groupPosition) {
        if (groupPosition != previousGroup)
            secondLevelExpListView.collapseGroup(previousGroup);
        previousGroup = groupPosition;
    }
});
return secondLevelExpListView;
}

@Override
public int getChildrenCount(int groupPosition) {
return 1;
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                     View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
    LayoutInflater layoutInflater = (LayoutInflater) this.mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.expandable_list_second, parent, false);
}
TextView lblListHeader = (TextView) convertView
        .findViewById(R.id.lblListHeader);
lblListHeader.setText(headerTitle);
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

 @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

在xml中新添加了(空数组的)字符串数组.可能有一个更优雅的解决方案,但我还不知道.

Newly added String-array (of an empty array) in the xml. There may be a more elegant solution but I don't know how yet.

这篇关于在expandableListView的第三层中填充不同的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆