0

Im trying to implement an expandable list view adapter.

I have this data that im representing in the adapter

private Map<Group, List<Contact>> groupedContactList = new HashMap<Group, List<Contact>>();

I overrode the getGroup method fine and it works but I am having trouble overriding the getChild method because the child is a list and im not sure if you're supposed to return the whole list or just one member in the list.

EDIT: entire class

public class MyGroupsAdapter extends BaseExpandableListAdapter {

    private LayoutInflater mInflater;
    private Context context;

    private Map<Group, List<Contact>> groupedContactList = new HashMap<Group, List<Contact>>();

    public MyGroupsAdapter(HashMap<Group, List<Contact>> groupedContactList,
            Context context) {
        this.groupedContactList = groupedContactList;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Contact getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition+1).getContactsInGroup().get(childPosition+1);
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        return arg0;
    }

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

        final String contactRow = (String) getChild(groupPosition,
                childPosition).getFullName();

        mInflater = LayoutInflater.from(context);

        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.groups_exp_list_view_child, null);
        }

        TextView item = (TextView) convertView.findViewById(R.id.ListItem);
        item.setText(contactRow);

        return convertView;
    }

    @Override
    public int getChildrenCount(int arg0) {
        return groupedContactList.size();
    }

    @Override
    public Group getGroup(int arg0) {
        // return
        // groupedContactList.get(HomeScreenActivity.groupsDB.getGroup(arg0+1));
        return (Group)groupedContactList.keySet().toArray()[arg0];
        //return HomeScreenActivity.groupsDB.getGroup(arg0 + 1);
    }

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

    @Override
    // /PROBABLY WRONGH
    public long getGroupId(int arg0) {
        return arg0;
    }

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

        final String groupName = getGroup(groupPosition).getName();

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.groups_exp_list_view_header, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.ListHeader);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(groupName);
        return convertView;

    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
4

1 回答 1

1

下面的代码对我有用:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;

public ExpandableListAdapter(Context pContext,
        ExpandableListView pExpandableListView,
        List<GroupEntity> pGroupCollection) {
    mContext = pContext;
    mGroupCollection = pGroupCollection;
    mExpandableListView = pExpandableListView;
    groupStatus = new int[mGroupCollection.size()];

    setListEvent();
}


private void setListEvent() {

    mExpandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 1;
                    mExpandableListView.setSelectedGroup(arg0);
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 0;
                }
            });
}

@Override
public String getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}

@Override
public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
        ViewGroup arg4) {
    // TODO Auto-generated method stub

    ChildHolder childHolder;
    if (arg3 == null) {
        arg3 = LayoutInflater.from(mContext).inflate(
                R.layout.list_group_item, null);

        childHolder = new ChildHolder();

        childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
        arg3.setTag(childHolder);
    } else {
        childHolder = (ChildHolder) arg3.getTag();
    }

    childHolder.title
            .setText(mGroupCollection.get(arg0).GroupItemCollection
                    .get(arg1).Name);

    return arg3;
}

@Override
public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(arg0).GroupItemCollection.size();
}

@Override
public Object getGroup(int arg0) {
    return mGroupCollection.get(arg0);
}

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

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

@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
    // TODO Auto-generated method stub
    GroupHolder groupHolder;
    if (arg2 == null) {
        arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                null);
        groupHolder = new GroupHolder();
        groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
        groupHolder.img1 = (FrameLayout) arg2.findViewById(R.id.tag_img1);
        groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
        arg2.setTag(groupHolder);
    } else {
        groupHolder = (GroupHolder) arg2.getTag();
    }
    if (groupStatus[arg0] == 0) {
        groupHolder.img.setImageResource(R.drawable.icn_max);
        groupHolder.img1
                .setBackgroundResource(R.drawable.content_bgparent1);

    } else {
        groupHolder.img.setImageResource(R.drawable.icn_min);
        groupHolder.img1.setBackgroundResource(R.drawable.active_bg);
    }
    groupHolder.title.setText(mGroupCollection.get(arg0).Name);

    return arg2;
}

class GroupHolder {
    ImageView img;
    FrameLayout img1;
    TextView title;
}

class ChildHolder {
    TextView title;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return true;
}

}

希望这可以帮助。

于 2013-10-17T04:57:07.420 回答