androidexpandablelistviewexpandablelistadapter

BaseExpandableListAdapter returns same child for every group position


I have a problem with ExpandableList, it shows all group elements in right order and names, but child element for every group is showing as the child element of the first gruop item I selected. No matter what I do child element stays the same for all groups. And every group has only one child.

This is a part of a JSON that I am using:

[
{
    "PK_Sevice_ID": 45,
    "Service_Name": "Interior",
    "Category_ID": 32,
    "Category_Name": "Legal",
    "Service_Model": {
        "PK_Sevice_ID": 45,
        "Service_Name": "Ministry of Interior",
        "Updated_At": "2019-05-20T00:00:00",
        "Service_Address": "Adress 1",
        "Open_Hours": null,
        "Contact_Details": "",
        "State_Name": "Serbia",
        "Services_Count": 1,
        "Longitude": "",
        "Latitude": "",
        "Service_Description": "",
        "Category_ID": 32,
        "Category_Name": "Legal",
        "Icon_Link": "law.png"
    }
},
{
    "PK_Sevice_ID": 47,
    "Service_Name": "UNHCR",
    "Category_ID": 32,
    "Category_Name": "Legal",
    "Service_Model": {
        "PK_Sevice_ID": 47,
        "Service_Name": "UNHCR",
        "Updated_At": "2019-05-20T00:00:00",
        "Service_Address": "Adress 2",
        "Open_Hours": null,
        "Contact_Details": "",
        "State_Name": "Serbia",
        "Services_Count": 1,
        "Longitude": "",
        "Latitude": "",
        "Service_Description": "Provides information",
        "Category_ID": 32,
        "Category_Name": "Legal",
        "Icon_Link": "law.png"
    }
},
{
    "PK_Sevice_ID": 48,
    "Service_Name": "Human Rights ",
    "Category_ID": 32,
    "Category_Name": "Legal",
    "Service_Model": {
        "PK_Sevice_ID": 48,
        "Service_Name": "Human Rights ",
        "Updated_At": "2019-05-20T00:00:00",
        "Service_Address": "Adress 3",
        "Open_Hours": null,
        "Contact_Details": "",
        "State_Name": "Serbia",
        "Services_Count": 1,
        "Longitude": "",
        "Latitude": "",
        "Service_Description": "A non governmental organization that provides professional free legal advice and assistance",
        "Category_ID": 32,
        "Category_Name": "Legal",
        "Icon_Link": "law.png"
    }
}
]

This is the adapter class:

public class AllServicesExpandableList extends BaseExpandableListAdapter {

private Context context;
private ArrayList <ParentServicesModel> mList;
private MapView mapView;
public AllServicesExpandableList(Context context, ArrayList <ParentServicesModel> list, MapView mMapView) {
    this.context = context;
    this.mList = list;
    this.mapView = mMapView;
}

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

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

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

@Override
public ServicesWithCategoryModel getChild(int groupPosition, int childPosition) {
    return mList.get(groupPosition).getServiceModel();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        

        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_header_service, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.txt_description);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item_service, null);
        TextView txt_address_description = convertView.findViewById(R.id.txt_address_description);
        TextView txt_telephones = convertView.findViewById(R.id.txt_telephones);
        TextView txt_hours_description = convertView.findViewById(R.id.txt_hours_description);
        txt_address_description.setText((CharSequence) getChild(groupPosition, childPosition).getServiceAddress());
        txt_telephones.setText((CharSequence) getChild(groupPosition, childPosition).getContactDetails());
        txt_hours_description.setText((CharSequence) getChild(groupPosition, childPosition).getOpenHours());
        
    }
    return convertView;
}

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


}

To make it simple I expect this data:

Group1 -Child1

Group2 -Child2

Group3 -Child3

But let's say I click on Group2 I will get this:

Group1 -Child2

Group2 -Child2

Group3 -Child2

Thank you for any suggestions


Solution

  • Try this:

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item_service, null);
        }
        TextView txt_address_description = convertView.findViewById(R.id.txt_address_description);
        TextView txt_telephones = convertView.findViewById(R.id.txt_telephones);
        TextView txt_hours_description = convertView.findViewById(R.id.txt_hours_description);
        txt_address_description.setText((CharSequence) getChild(groupPosition, childPosition).getServiceAddress());
        txt_telephones.setText((CharSequence) getChild(groupPosition, childPosition).getContactDetails());
        txt_hours_description.setText((CharSequence) getChild(groupPosition, childPosition).getOpenHours());
        
        return convertView;
    }