Search code examples
androidandroid-arrayadapterexpandablelistviewandroid-gridviewexpandablelistadapter

Expandable listview with gridview inside on Android


I'm trying to pass a array of data to my GridAdapter so I can get that and print as a gridview, the problem is that my current array is returning null, what am I doing wrong here? how can I get the arraylist of child and pass it to my GridAdapter? Thank you in advance.

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;
    private ArrayList<Child> child;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }



    @Override
    public Object getChild(int groupPosition, int childPosition) {
        child = groups.get(groupPosition).getItems();
        return child.get(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) {

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.gridview, null);
        }
        CustomGridView gridView = (CustomGridView) convertView
                .findViewById(R.id.GridView_toolbar);

        gridView.setNumColumns(3);// gridView.setGravity(Gravity.CENTER);//
        gridView.setHorizontalSpacing(5);// SimpleAdapter adapter =
        GridAdapter adapter = new GridAdapter(context, **child**);
        gridView.setAdapter(adapter);// Adapter

        int totalHeight = 0;
        for (int size = 0; size < adapter.getCount(); size++) {
            RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(
                    size, null, gridView);
            TextView textView = (TextView) relativeLayout.getChildAt(0);
            textView.measure(0, 0);
            totalHeight += textView.getMeasuredHeight();
        }
        gridView.SetHeight(totalHeight);
        return convertView;

    }

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

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

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

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

}

Solution

  • take child array as

    ArrayList<ArrayList<Child>> child = new ArrayList<>();
    

    and initialize it on constructor call

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
        for(int i = 0; i < groups.size(); i++){
            child.add(i, groups.get(i).getItems());
        }
    }
    

    and remove

    child = groups.get(groupPosition).getItems();
    

    from getChild method