Search code examples
androidexpandablelistviewcustom-adapterexpandablelistadapter

Populate expandable listview with custom list objects in Android


I have list of custom objects and I want to populate ExpandableListView with data of these objects . But at the custom adapter it stuck at getGroupCount function and it stops without error and not passing another function like getChildrenCount. First getGroupCount is working and after getGroupCount but nothing else. Expandablelistview is not showing at the screen. No error is shown and no explistview is shown, I cant find the problem, I see my list s have data. Adapter is like below,

public class salItemsAdapter extends BaseExpandableListAdapter {

public List<ABSSALEITEM> extSetItems;
public Context context;
public LayoutInflater inflater;
public HashMap<Integer, List<ABSSALEITEM>> extSubSetItems;

public TextView txtSalItemHeaderClm1;
public TextView txtSalItemHeaderClm2;
public TextView txtSalItemHeaderClm3;
public TextView txtSalItemBottomText;


public salItemsAdapter(Context context, List<ABSSALEITEM> extSetItems, HashMap<Integer, List<ABSSALEITEM>> extSubSetItems) {
    this.context = context;
    this.extSetItems = extSetItems;
    this.extSubSetItems = extSubSetItems;
}
@Override
public int getChildrenCount(int groupPosition) {
    return extSubSetItems.get(extSetItems.get(groupPosition)).size();
}

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

@Override
public Object getGroup(int groupPosition) {
    return extSetItems.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return extSubSetItems.get(extSetItems.get(groupPosition)).get(childPosition);
}

@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 view, ViewGroup parent)  {
    ABSSALEITEM setSalItem = (ABSSALEITEM)getGroup(groupPosition);

    if(view == null)
    {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.salitems_elv_head, parent, false);
    }

    txtSalItemHeaderClm1 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm1);
    txtSalItemHeaderClm2 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm2);
    txtSalItemHeaderClm3 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm3);

    txtSalItemHeaderClm1.setText(setSalItem.MAT);
    txtSalItemHeaderClm2.setText(setSalItem.EXPLAIN);
    txtSalItemHeaderClm3.setText(Double.toString(setSalItem.QUANT));

    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View view, ViewGroup parent) {
    ABSSALEITEM setSalItem = (ABSSALEITEM)getChild(groupPosition, childPosition);
    if(view==null)
    {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.salitems_elv_item, parent, false);
    }

    txtSalItemBottomText = (TextView)view.findViewById(R.id.txtSalItemBottomText);
    txtSalItemBottomText.setText(setSalItem.LONGEXP);
    return view;
}

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

And I fill my data objects like

    private void FillExpListView(List<ABSSALEITEM> paSalSetItems, List<ABSSALEITEM> paSalSetSubItems) {

    hbSubSetItems = new HashMap<Integer, List<ABSSALEITEM>>();

    List<ABSSALEITEM> tmp = new ArrayList<ABSSALEITEM>(paSalSetSubItems);
    List<ABSSALEITEM> tmp2 = new ArrayList<ABSSALEITEM>();
    Integer tmpItemnum = 0;

    for (ABSSALEITEM tmpSalItem : tmp) {

        tmpItemnum = tmpSalItem.ITEMNUM;

        if(!hbSubSetItems.containsKey(tmpItemnum))
        {
            tmp2.clear();
            for (ABSSALEITEM tmpSalItem2 : paSalSetSubItems) {
                if (tmpItemnum == tmpSalItem2.ITEMNUM) {
                    tmp2.add(tmpSalItem2);
                }
            }
            hbSubSetItems.put(tmpSalItem.ITEMNUM, tmp2);
        }
    }

    adapterSalItems = new salItemsAdapter(salitems.this, paSalSetItems, hbSubSetItems);
    elvSalItems.setAdapter(adapterSalItems);
}

Thanks for help


Solution

  • You can use libraries which are available on GitHub. Create two layouts :- one for the heading (item on which user click and it expands ) and other one for the items which are visible on clicking the heading.

    Some Libraries are

    enter image description here

    enter image description here

    enter image description here