Search code examples
androidarraylistandroid-arrayadapterexpandablelistview

Getting error in child view of Expandable listview


I would like to use ExpandableListView in my application. Untill now everything works fine, except the childView. All the childViews are getting matched with each and every single parentView.

public class AdTypesAdapter extends BaseExpandableListAdapter {

Context context;
public LayoutInflater minflater;
ArrayList<Q2_AdTypeList> adTypeList;

public AdTypesAdapter(Context context, ArrayList<Q2_AdTypeList> adTypeList) {
    // TODO Auto-generated constructor stub
    this.adTypeList = adTypeList;
    context = context;
}

public void setInflater(LayoutInflater mInflater, Context context) {
    this.minflater = mInflater;
    context = context;
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return adTypeList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return adTypeList.size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return adTypeList.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.ad_details_parent_layout,
                null);
    }

    TextView noOfImpressions_textView = (TextView) convertView
            .findViewById(R.id.noOfImpressions_textView);
    TextView amount_textView = (TextView) convertView
            .findViewById(R.id.amount_textView);
    noOfImpressions_textView.setText(String.valueOf(adTypeList.get(
            groupPosition).getAdvImpression()));
    amount_textView.setText("$"
            + String.valueOf(adTypeList.get(groupPosition).getAdvPrice()));

    // ((TextView) convertView).setChecked(isExpanded);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    TextView adDesc_textView = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.ad_details_child_layout,
                null);
    }
    adDesc_textView = (TextView) convertView
            .findViewById(R.id.adDesc_textView);

    adDesc_textView.setText(adTypeList.get(childPosition).getAdvDesc());
    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return false;
}

}

Here the adTypeList consists of data for both parent and childview![ I would like to get exactly what it is in the image. But what I get in my output is that the total number of childview I have is attached to each and every parentview]1


Solution

  • I think your getChildrenCount should always return 1 based on your data model (each parent has exactly 1 child)

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

    By returning adTypeList.size() you basically say that the group at position groupPosition has adTypeList.size() children.