Search code examples
androidandroid-volleyexpandablelistviewexpandablelistadapter

How to map childView in Expandable ListView using volley


I am making expandable listview in android using Volley. I have mapped parentView Successfully. But I am facing problem how I map childView in an expandable listview. in my situation, all subcategories are shown in a single parent category. How I can handle this. How I can map properly childView in the expandable listview. here is a snapshot of lists which I have shown in logcat.

screenshot : child caetgory

here is my code from which I am getting a response from JSON.

String ChildName = null;
JSONObject object = null,childObject= null;
try {
    object =new JSONObject(response);
    JSONArray headerArray = object.getJSONArray("ListCategories");
    for (int i=0;i<headerArray.length();i++){
        JSONObject jsonObject = headerArray.getJSONObject(i);
        final int Id = jsonObject.getInt("CategoryID");
        final String name = jsonObject.getString("CategoryName");

        JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
        for (int j=0;j<childArray.length();j++) {
            childObject = childArray.getJSONObject(j);
            ChildId = childObject.getInt("ID");
            ChildName = childObject.getString("Name");
            child.add(new ChildCategory(ChildId, ChildName));
        }
        Log.e("childArray", childArray.toString());
        header.add(new Category(Id, name));
    }

} catch (JSONException e) {
    e.printStackTrace();
}
adapter = new ExpandableCategoryAdapter(context,header,child);
expandableListView.setAdapter(adapter);
adapter.notifyDataSetChanged();

here is expandable categoryAdapter:

public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {


    private Context _context;
    private ArrayList<Category> header;
    private ArrayList<ChildCategory> childCategories;
    private HashMap<String, String> child;
    final String name = null;
    private TextView header_text;

    public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ChildCategory> listDataChild) {
        this._context = context;
        this.header = listDataHeader;
        this.childCategories = listDataChild;
    }

    @Override
    public int getGroupCount() {
        // Get header size
        return header.size();

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childCategories.size();
    }

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

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

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

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

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
        }
        TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
        child_text.setText(child.getName());
        return convertView;
    }
    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Category category = (Category) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
        }
        header_text = (TextView) convertView.findViewById(R.id.listTitle);
        header_text.setText(category.getCategoryName());
        if (isExpanded) {
            header_text.setTypeface(null, Typeface.BOLD);
            header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
        } else {
            header_text.setTypeface(null, Typeface.NORMAL);
            header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
        }

        return convertView;
    }

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

I don't know where I am doing the mistake. thank you in advance


Solution

  • The child list should be break up according to header/group, so there is a allChild list as ArrayList<ArrayList<ChildCategory>>. Try this:

    String ChildName = null;
    JSONObject object = null,childObject= null;
    ArrayList<ArrayList<ChildCategory>> allChild = new ArrayList<ArrayList<ChildCategory>>(); // Added
    try {
        object =new JSONObject(response);
        JSONArray headerArray = object.getJSONArray("ListCategories");
        for (int i=0;i<headerArray.length();i++){
            JSONObject jsonObject = headerArray.getJSONObject(i);
            final int Id = jsonObject.getInt("CategoryID");
            final String name = jsonObject.getString("CategoryName");
    
            JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
            child = new ArrayList<ChildCategory>(); // Added
            for (int j=0;j<childArray.length();j++) {
                childObject = childArray.getJSONObject(j);
                ChildId = childObject.getInt("ID");
                ChildName = childObject.getString("Name");
                child.add(new ChildCategory(ChildId, ChildName));
            }
            Log.e("childArray", childArray.toString());
            allChild.add(child); // Added
            header.add(new Category(Id, name));
        }
    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    adapter = new ExpandableCategoryAdapter(context,header,allChild); // Changed
    expandableListView.setAdapter(adapter);
    

    The adapter:

    public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
    
    private Context _context;
    private ArrayList<Category> header;
    private ArrayList<ArrayList<ChildCategory>> childCategories; // Changed
    private HashMap<String, String> child;
    final String name = null;
    private TextView header_text;
    
    public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ArrayList<ChildCategory>> listDataChild) { //Changed
        this._context = context;
        this.header = listDataHeader;
        this.childCategories = listDataChild;
    }
    
    @Override
    public int getGroupCount() {
        // Get header size
        return header.size();
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return childCategories.get(groupPosition).size(); // Changed
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        return header.get(groupPosition);
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childCategories.get(groupPosition).get(childPosition); // Changed
    }
    
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
        }
        TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
        child_text.setText(child.getName());
        return convertView;
    }
    
    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Category category = (Category) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
        }
        header_text = (TextView) convertView.findViewById(R.id.listTitle);
        header_text.setText(category.getCategoryName());
        if (isExpanded) {
            header_text.setTypeface(null, Typeface.BOLD);
            header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
        } else {
            header_text.setTypeface(null, Typeface.NORMAL);
            header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
        }
    
        return convertView;
    }
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    }
    

    Hope that helps!