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