如何从数据库中填充ExpandableListView?

时间:2014-09-25 11:49:31

标签: android adapter expandablelistview android-adapter

我想在ExpandableListView中显示数据,以便当用户展开每个项目时,我想显示有关该项目的额外信息。是ExpandableListView可以吗? 我用这种方式实现了BaseExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<Idiom> _listDataHeader;
    private List<Idiom> _child;
    public ExpandableListAdapter(Context context, List<Idiom> listDataHeader, List<Idiom> idioms) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._child = idioms;
    }

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

    @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) {

        Idiom idiom = _listDataHeader.get(groupPosition);

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

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(idiom.getVerb_fa());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataHeader.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) {
        Idiom idiom = (Idiom) getGroup(groupPosition);

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

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

        return convertView;
    }

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

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

我的模特:

public class Idiom {
    public long id;
    public String verb;
    public String meaning;
    public String description;
    public String verb_fa;
}

但是当我点击列表中的每个项目时,它的孩子会重复几次。实际上结果应该是这样的: enter image description here

任何想法?

0 个答案:

没有答案