0

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.

this is the

  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;
    }
}

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

Kinjal
  • 1,195
  • 4
  • 13
  • 24
rogue_shadow
  • 95
  • 2
  • 13
  • @kinjal could you possibly help me with my problem. I see you've edited my question. I would be really glad if you could help me as well. – rogue_shadow Jan 29 '17 at 01:51

1 Answers1

0

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.

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;
}
}

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.

jom
  • 11
  • 4
  • 2
    Please explain how this solves the OP's problem. Are you the same person as the OP? – Code-Apprentice Sep 11 '17 at 00:26
  • If you scroll through the code u should find "//newly inserted Code" and "//end of new Code" which indicates the newly added section. This polulates the third level list view and solves Op´s problem and no i am not the Op. – jom Sep 11 '17 at 10:40
  • You should edit your question to explain this more clearly. – Code-Apprentice Sep 11 '17 at 12:46
  • Hope this is understandable now. The code is actually not that hard to find out, but for a beginner like me or the Op this was hard :D and i thought I d share the solution for future beginners. – jom Sep 11 '17 at 14:10