2012-11-14 1 views
1

나는 정말 ExpandableListView에서 groupPosition과 childPosition을 얻는 방법을 잘 모르겠습니다. 스위치와 케이스를 사용해 보았지만 작동하지 않는다면 groupPosition을 인식하지 못하는 것처럼 보입니다. 그렇다면 어떻게 groupPosition과 childPosition을 얻을 수 있습니까?andChildClick, groupPosition 및 childPosition을 Android에서 ExpandableListView에서 가져 오는 방법은 무엇입니까?

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    // Set up our adapter 
    mAdapter = new MyExpandableListAdapter(); 
    setListAdapter(mAdapter); 
    registerForContextMenu(getExpandableListView()); 

    getExpandableListView().setOnChildClickListener(new OnChildClickListener() 
    { 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 
     { 
      switch(groupPosition & childPosition) 
      { 
      case 2 & 2: 
       Intent asariIntent = new Intent(Codex.this, CodexAsari.class); 
       Codex.this.startActivity(asariIntent); 
       break; 

      case 1 & 0: 
       Intent protheanIntent = new Intent(Codex.this, CodexProthean.class); 
       Codex.this.startActivity(protheanIntent); 
       break; 

      case 1 & 1: 
       Intent rachniIntent = new Intent(Codex.this, CodexRachni.class); 
       Codex.this.startActivity(rachniIntent); 
       break; 
      } 
      return false; 
     } 
    }); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    menu.setHeaderTitle("Sample menu"); 
    menu.add(0, 0, 0, "Sample action"); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); 

    String title = ((TextView) info.targetView).getText().toString(); 

    int type = ExpandableListView.getPackedPositionType(info.packedPosition); 
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
     Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos, 
       Toast.LENGTH_SHORT).show(); 
     return true; 
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return false; 
} 

public class MyExpandableListAdapter extends BaseExpandableListAdapter 
{ 
    public String[] groups = { "Aliens: Council Races", "Aliens: Extinct Races", "Aliens: Non-Council Races", "Aliens: Non-Sapient Creatures", "Citadel and Galactic Government", "Humanity and the Systems Alliance", "Known Associates" }; 
    private String[][] children = { 
      { "Asari", "Salarians", "Turians" }, 
      { "Protheans", "Rachni" }, 
      { "Batarians", "Collectors", "Drell", "Elcor", "Geth", "Hanar", "Keepers", "Krogan", "Praetorians", "Quarians", 
       "Reapers", "Reapers: Indoctrination", "Sovereign", "Volus", "Vorcha" }, 
      { "Husks", "Thresher Maws", "Varren" }, 
      { "Citadel", "Citadel Council", "Citadel Space", "Spectres" }, 
      { "Councilor Donnel Udina: Anderson Chosen Councilor", "Councilor Donnel Udina: Undina Chosen Councilor", "Earth: Mass Effect", "Earth: Mass Effect 3", "First Contact War", "Systems Alliance", "Rise of the Alliance: Renegade", "Rise of the Alliance: Paragon" }, 
      { "Steven Hackett", "David Anderson", "Udina Chosen Councilor", "Dr. Liara T'Soni", "EDI", "Garrus Vakarian", "Kaidan Alenko", "Ashley Williams", "Tali'Zorah", "Dr. Mordin Solus", "Jacob Taylor", "Jeff 'Joker' Moreau", "Miranda Lawson", "Dr. Karin Chakwas" } 
    }; 

    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

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

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public TextView getGenericView() { 
     AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT, 64); 

     TextView textView = new TextView(Codex.this); 
     textView.setLayoutParams(lp); 
     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
     textView.setPadding(36, 0, 0, 0); 
     return textView; 
    } 

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getChild(groupPosition, childPosition).toString()); 
     return textView; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getGroup(groupPosition).toString()); 
     return textView; 
    } 

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

    public boolean hasStableIds() { 
     return true; 
    } 



} 

public int childPosition() 
{ 
    return 0; 
} 

답변

0

내가 그런 말을하는 사례를 본 적이 없다. 아마도 당신은 다음을 시도하고 당신이 어떤 다른 결과를 얻을 수 있는지 볼 수 있습니다 :

getExpandableListView().setOnChildClickListener(new OnChildClickListener() { 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
     switch(groupPosition) { 
     case 1: 
      switch (childPosition) { 
      case 0: 
       Intent protheanIntent = new Intent(Codex.this, CodexProthean.class); 
       Codex.this.startActivity(protheanIntent); 
       break;    
      case 1: 
       Intent rachniIntent = new Intent(Codex.this, CodexRachni.class); 
       Codex.this.startActivity(rachniIntent); 
       break; 
      } 
     case 2: 
      switch (childPosition) { 
      case 2: 
       Intent asariIntent = new Intent(Codex.this, CodexAsari.class); 
       Codex.this.startActivity(asariIntent); 
       break; 
      } 
     } 
     return false; 
    } 
}); 
0

MyExpandableListAdapter 클래스에서 getChildView()을 무시하십시오. getChildView은 자식이 터치 될 때마다 호출됩니다 (또는 체크 박스가 선택되거나 선택 취소되었을 때 - 나는 기억하지 못하거나 차이가있는 경우). 이 메서드 내에서 groupPosition 및 childPosition에 액세스 할 수 있습니다. 자세한 내용은 Working with children in ExpandableListActivity을 참조하십시오.

관련 문제