How to use Expanadable listview with custom class object in framgent?

897 views Asked by At

I have one fragment in which i have placed the one Expandable list view. I have one modal class which have the list of products. In Expandable list view,on header i want to set the section name which i have placed successfully, and as a child i want to placed the products which comes under the different section name.

But i got the only one products when i have applied my logic. This is what i got....!

enter image description here

Here i have got the only 1 product name which repeated in all products.

Here what i have tried so far ::

Adapter ::

class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        private ArrayList<String> _listDataHeader; // header titles
        ArrayList<Products> topupProducts;

        public ExpandableListAdapter(Context context, ArrayList<String> listDataHeader,ArrayList<Products> topupProducts) 
        {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this.topupProducts = topupProducts;
        }

        @Override
        public Products getChild(int groupPosition, int childPosititon) {

            /*return this.topupProducts.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);*/
            return topupProducts.get(childPosititon);
        }

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

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

            // child =  topupProducts.get(groupPosition).getProductName();
            //final String childText = (String) getChild(groupPosition, childPosition);
            final String childText = topupProducts.get(groupPosition).getProductName();

            if (convertView == null)
            {
                LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item_fragment, null);
            }

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(_context, topupProducts.get(childPosition).getProductID(),Toast.LENGTH_SHORT).show();
                }
            });

            TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
            txtListChild.setText(childText);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return this.topupProducts.size();
            //return ((ArrayList<String>) topupProducts.get(groupPosition)).size();
        }

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

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

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) 
        {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) 
            {
                LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);

            return convertView;
        }

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

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

Any help will be appreciated.. Thanks in Advance...

EDIT ::

This is how i got the my product section Name from Local Database(SQlite)

listHeader = new LinkedHashMap<String, String>();
DataHelper dataHelper=new DataHelper(ctx);
topupProSectionsName=new ArrayList<String>();
topupProSectionID=new ArrayList<String>();
listHeader= dataHelper.getSectionSForTopupProduct();

if (listHeader != null) 
{
    Set entrySet = listHeader.entrySet();
    Iterator i = entrySet.iterator();
    while (i.hasNext()) 
    {
        Map.Entry me = (Map.Entry)i.next(); 
        topupProSectionsName.add((String) me.getKey());
        topupProSectionID.add((String) me.getValue());
         addProduct((String)me.getKey(),listDataChild);

    }
}

MyModal Class ::

public class TopupProSectionName 
{
    private String sectionName;
    private ArrayList<Products> topupProductList = new ArrayList<Products>();

    public String getsectionName() {
        return sectionName;
    }
    public void setsectionName(String sectionName) {
        this.sectionName = sectionName;
    }

    public ArrayList<Products> gettopupProductList() {
        return topupProductList;
    }
    public void settopupProductList(ArrayList<Products> topupProductList) {
        this.topupProductList = topupProductList;
    }
}

AddProduct Method ::

private int addProduct(String department,ArrayList<Products> product)
    {
        int groupPosition = 0;

        TopupProSectionName headerInfo = myDepartments.get(department);
        if(headerInfo == null)
        {
            headerInfo = new TopupProSectionName();
        }
        headerInfo.setsectionName(department);
        myDepartments.put(department, headerInfo);
        deptList.add(headerInfo);

        ArrayList<Products> productList = headerInfo.gettopupProductList();
        int listSize = productList.size();
        listSize++;

        Products detailInfo = new Products();
        detailInfo.setProductName(product.get(groupPosition).getProductName());
        productList.add(detailInfo);
        headerInfo.settopupProductList((productList));

        groupPosition = deptList.indexOf(headerInfo);
        return groupPosition;
    }

Modified Expandable Adapter ::

class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        ArrayList<Products> topupProducts;
         ArrayList<TopupProSectionName> listDataHeader;
        public ExpandableListAdapter(Context context, ArrayList<TopupProSectionName> listDataHeader)
        {
            this._context = context;
            this.listDataHeader = listDataHeader;
            this.topupProducts = topupProducts;
        }

        @Override
        public Products getChild(int groupPosition, int childPosititon) {

            ArrayList<Products> productList = deptList.get(groupPosition).gettopupProductList();
            return productList.get(childPosititon);
        }

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

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


            final Products childText = (Products) getChild(groupPosition, childPosition);


            if (convertView == null)
            {
                LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item_fragment, null);
            }

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(_context, topupProducts.get(childPosition).getProductID(),Toast.LENGTH_SHORT).show();
                }
            });

            TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
            txtListChild.setText(childText.getProductName());
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {

            ArrayList<Products> productList = deptList.get(groupPosition).gettopupProductList();
            return productList.size();
        }

        @Override
        public Object getGroup(int groupPosition) {

            return deptList.get(groupPosition);
        }

        @Override
        public int getGroupCount() {

             return deptList.size();
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) 
        {

            TopupProSectionName headerTitle = (TopupProSectionName) getGroup(groupPosition);
            if (convertView == null) 
            {
                LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle.getsectionName());

            return convertView;
        }

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

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

There are 1 answers

2
Aprian On

The structure would be something like this:

List<String> groupList = new ArrayList<String>();
groupList.add("Group 1");
groupList.add("Group 2");

List<List<String>> childList = new ArrayList<List<String>>();
List<String> group1Childs = new ArrayList<String>();
group1Childs.add("Child 1 of Group 1");
group1Childs.add("Child 2 of Group 1");
childList.add(group1Childs);
List<String> group2Child = new ArrayList<String>();
group2Childs.add("Child 1 of Group 2");
group2Childs.add("Child 2 of Group 2");
childLists.add(group2Childs);

In your Adapter:

public int getChildCount(int groupPosition) {
    return childList.get(groupPosition).size();
}

public String getChild(int groupPosition, int childPosition) {
    return childList.get(groupPosition).get(childPosition);
}

Hope you get what I meant. This is just a simple example of Expandable List, in your case you should change the String to Products.

What I did

I once used a single List structure like:

public class ExpandableItem {
    private String groupName;
    private List<String> childs;
}

Then I passed List<ExpandableItem> to Adapter constructor as _items

public int getChildCount(int groupPosition) {
    return _items.get(groupPosition).getChilds().size();
}

public String getChild(int groupPosition, int childPosition) {
    return _items.get(groupPosition).getChilds().get(childPosition);
}

I don't know whether this is a good practice, but it works.