Wednesday, 19 May 2021

Android how to get parent name of a child on click of expandablelistview

I have created a 3-level ExpandableListView like below

enter image description here

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.



from Android how to get parent name of a child on click of expandablelistview

No comments:

Post a Comment