如何在片段中使用Expandablelistview和自定义类对象?

时间:2013-12-03 07:08:11

标签: android expandablelistview custom-object

我有一个片段,其中我放置了一个可扩展列表视图。 我有一个模态类,其中包含产品列表。 在可扩展列表视图中,在标题上我想设置已成功放置的节名称, 作为一个孩子,我想放置不同部分名称下的产品。

但是当我应用我的逻辑时,我得到了唯一的产品。 这就是我得到的......!

enter image description here

在这里,我获得了所有产品中唯一重复的产品名称。

这是我到目前为止所尝试的内容::

适配器::

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;
        }
    }

任何帮助将不胜感激.. 在此先感谢...

编辑::

这就是我从本地数据库(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;
    }

修改的可扩展适配器::

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 个答案:

答案 0 :(得分:2)

结构将是这样的:

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);

在适配器中:

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

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

希望你明白我的意思。这只是可扩展列表的一个简单示例,在您的情况下,您应该将String更改为Products

我做了什么

我曾经使用过一个List结构,如:

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

然后我将List<ExpandableItem>传递给Adapter构造函数_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);
}

我不知道这是否是一个好习惯,但它确实有效。