2016-07-19 2 views
0

GUI를 ExpandableListView으로 설계했습니다. 아래 샘플 그림을 참조하십시오. 이 상위 그룹에 의존 할 필요없이 목록에있는 모든 아이의 IDs을 얻을 수있는 경우상위 그룹에 의존하지 않고 ExpandableListView에서 모든 하위 ID를 얻는 방법

Screenshot of the GUI

나는 알고 싶어요. 내가 얻을 수있어 무엇 현재

첫 번째 부모를위한 0-2이고 두 번째 부모는 0

에서 시작 그래서 기본적으로 내가 같은 ID를 얻으려면 :

  • 기구를 ? 이드 (0)
  • 칠러? id (1)
  • 디스플레이? ID (2)
  • "33"내보내기 (수) ID (3) 여기 등

Custom adapter 클래스 :

public class MTOExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public MTOExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
    } 

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

    @Override 
    public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
    } 

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

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

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
    } 

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.list_mto_group, null); 
    } 

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

    return convertView; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final String childText = (String) getChild(groupPosition, childPosition); 

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

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblMTOListItem); 
    txtListChild.setText(childText); 

    return convertView; 
    } 

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

'BaseExpandableListAdapter' 하위 클래스와 같은 맞춤 어댑터를 사용하고 있습니까? 당신이 그 코드를 게시 할 수 있습니까? 일반적으로 이와 같은 요구 사항이있을 때 어댑터에 비위 재 정의 메소드를 추가하면됩니다. 'getAllChildIds'를 사용하고 그것을 사용하십시오. –

+0

@krislarson 맞춤 어댑터가있는 수업을 참조하십시오. –

답변

0

좋아, 난 당신이 요구하는 기능을 참조하십시오. 번호순으로 모든 그룹의 모든 하위 그룹에 대해 고유 한 ID를 얻으려고합니다.

@Override 
    public long getChildId(int groupPosition, int childPosition) { 
     int id = 0; 
     for (int group = 0; group < groupPosition; group++) { 
      id += getChildrenCount(group); 
     } 
     id += childPosition; 
     return id; 
    } 
+0

안녕하세요, Kris larson, 귀하의 답변을 시도했지만 잘못 된 것 같습니다. 내 질문과 함께 게시 한 스크린 샷을 보면 첫 3 명의 자녀를 클릭했을 때 0,1,2가 맞습니다.하지만 "33"내보내기 (할 수 있음) "를 클릭하면 id가 3 대신 id가 7로 점프. NB : getGroupCount() 메소드에서 그룹을 제거했습니다. 메소드에 매개 변수가 필요하지 않으므로. –

+0

완벽하게 작동했습니다. –

+0

또한 각 아이를 클릭 한 후에 배경색을 설정할 수 있는지 물어보고 싶습니다. getChildView() 메서드를 호출하여이 작업을 수행했지만 처음 세 자식 중 하나를 클릭하면 배경을 설정하지만 클릭하지 않은 다른 자식도 설정합니다. –

관련 문제