I have created a 3-level ExpandableListView like below
Below is my code
ThreeLevelListAdapter
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
SecondLevelAdapter
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
List<String[]> data;
String[] headers;
ImageView ivGroupIndicator;
public SecondLevelAdapter(Context context, String[] headers, List<String[]> data) {
this.context = context;
this.data = data;
this.headers = headers;
}
@Override
public Object getGroup(int groupPosition) {
return headers[groupPosition];
}
@Override
public int getGroupCount() {
return headers.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.rowSecondText);
String groupText = getGroup(groupPosition).toString();
text.setText(groupText);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String[] childData;
childData = data.get(groupPosition);
return childData[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) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView textView = convertView.findViewById(R.id.rowThirdText);
textView.setOnClickListener(v -> {
// here I want to get the all parent names of the child clicked
Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
});
String[] childArray = data.get(groupPosition);
String text = childArray[childPosition];
textView.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
String[] children = data.get(groupPosition);
return children.length;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
What I want to do?
I want to get all parent names when a child is clicked. For example, as shown in the figure above. If I click on M/S PAK MEDICOSE PHARMACY or M/S SERVAID PHARMACY then I want to get their parent names LICE-O-NIL CREAM and 1112 STOP TOWNSHIP and so on.
TextView textView = convertView.findViewById(R.id.rowThirdText);
textView.setOnClickListener(v -> {
//Here I want to get the names of parent
Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
});
How can I achieve this?
Any help would be highly appreciated.

Step 1: You already have a list of the elements that you are showing in the recycler view. Now create a list of these lists okay (I will tell you later why?)
Step 2: What you can do is create another global list (static also if used very often). Tip: if adapters are in other classes then pass the list through constructors.
Explanation: This list will save the clicked item's indexes (I am assuming that we do not know how many sublists are there in the recycler view). When the user clicks the item from the first recycler view list then we will save the index by (~adapter.getAdapterPosition ()) then the user clicks on the sublist then we save that index also and so on...
Now, suppose the user clicked like this:
So what we will do now, we have the list which contains an index and another list which contains n lists (where n = number of sublists). listsArrayList contains the list in order and the indexClickedList contains the index of element we clicked and the index in the indexClickedList is the number of list from which we clicked the item so you can access that from the listsList. In this way, you can get the parent item of the element we clicked. Here you have to use a while loop because we don't know how many sublists are there. You go on until we reach list.get(whatEverIndex == null).
Whenever we have branches we always use tree structures, In the first comment I thought you had only 1 sublist but now in your case we have multiple. So you should definitely use tree structure. Be careful while making the loops, you may get null pointer exceptions.