2013-10-16 163 views
0

我试图实现可扩展列表视图适配器。Android:可扩展列表视图实现

我有这样的数据,即时通讯在适配器

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

我推翻了getGroup法测得的代表,它的作品,但我无法重写getChild方法,因为孩子是一个列表,即时通讯不知道你应该返回整个列表或列表中的一个成员。

编辑:全班

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

} 
+0

你可以发布整个班级吗? – Harshid

+0

@Harshid我发布了它。 getChild不起作用 – Ogen

回答

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

}

希望这有助于。