2013-10-16 2 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

@ 하 시드 나는 그것을 게시했다. 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; 
} 

}

희망을.