2011-03-03 1 views
3

네트워크를 통해 외부 데이터를 가져오고 ExpandableListAdapter을 통해 ExpandableListView을 채우는 간단한 예제를 작성하려고합니다. 나는 몇 가지 예제를 시도하고 꽤 많이 잃어 버렸다. 나는 ArrayAdapter 클래스로 작업하는 것에 익숙하지만, ExpandableListAdapter은 다른 것 같습니다.ExpandableListAdapter를 알 수없는 것처럼 보일 수 없습니다.

MyExpandableListAdapter :

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 

    public static class GroupHolder { 
     TextView title; 
    } 

    public static class ChildHolder { 
     TextView title; 
     ImageView icon; 
    } 


    // groups.getChildren() returns the children 
    private List<Group> groups; 

    private Context context; 

    private LayoutInflater inflater; 

    public MyExpandableListAdapter(Context context, List<Group> groups) { 
     this.context = context; 
     this.groups = groups; 
     this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

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

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

    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     GroupHolder holder; 

     if (convertView != null) { 
      holder = (GroupHolder)convertView.getTag(); 
     } else { 
      convertView = inflater.inflate(R.layout.group_item, parent, false); 

      holder = new GroupHolder(); 
      holder.title = (TextView) convertView.findViewById(R.id.group_item_title); 

      convertView.setTag(holder); 
     } 

     holder.title.setText(this.groups.get(groupPosition).getName()); 

     return convertView; 
    } 

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

    public Object getChild(int groupPosition, int childPosition) { 
     return groups.get(groupPosition).getChildren().get(childPosition); 
    } 

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

    public int getChildrenCount(int groupPosition) { 
     return groups.get(groupPosition).getChildren().size(); 
    } 

    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     ChildHolder holder; 

     if (convertView != null) { 
      holder = (ChildHolder) convertView.getTag(); 
     } else { 
      convertView = inflater.inflate(R.layout.child_item, parent, false); 

      holder = new ChildHolder(); 
      holder.title = (TextView) convertView.findViewById(R.id.child_item_title); 
      holder.icon = (ImageView) convertView.findViewById(R.id.child_item_icon); 

      convertView.setTag(holder); 
     } 

     holder.title.setText(groups.get(groupPosition).getChildren().get(childPosition).getName()); 
//  TODO add in image loading. 

     return convertView; 
    } 
} 

MyExpandableListActivity :

public class MyExpandableListActivity extends ExpandableListActivity { 

    private List<Group> groups; 

    private ExpandableListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.group_list); 

     this.groups = new ArrayList<Group>(); 
     Group group = new Group("$1", "Colors"); 
     Child child = new Child("$2", "Red"); 
     groups.getChildren().add(child); 

     this.adapter = new MyExpandableListAdapter(this, groups); 

     setListAdapter(this.adapter); 
    } 
} 

R.layout.group_list :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <ExpandableListView android:id="@+id/android:list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 

    <TextView android:id="@+id/android:empty" 
      android:text="EMPTY! DOOM!" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
</LinearLayout> 
여기 내 현재 응용 프로그램 코드가 표시되어 아무것도 아니다 6,

R.layout.group_item :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <TextView android:id="@+id/group_item_title"/> 
</LinearLayout> 

R.layout.child_item :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

    <ImageView android:id="@+id/child_item_icon" 
      android:layout_width="48px" android:layout_height="48px"/> 

    <TextView android:id="@+id/child_item_title"/> 
</LinearLayout> 

나는 물론, "보는 EMPTY있다! DOOM! "이라는 메시지가 표시됩니다. 내가 뭘 잘못 했나요?

또한 SimpleExpandableListAdapter은 일을 쉽게하기로되어있는 것 같지만 완전히 작동하는지 그것을 사용하는 방법 누군가가 나를 알아내는 데 도움이 될 수 있습니까?

답변

0

"fill_parent"를 동시에 사용하고 동일한 공간에서 사용하려고하는 두 개의 항목으로 이번 주에 조금 어려워졌습니다. 뒤로 또는 화면 꺼짐. 그룹 목록 레이아웃에서 비슷한 설정을 볼 수 있습니다. 보이지 않는 목록 부분에 도움이 되길 바랍니다.

+0

는 match_parent''에 그 레이아웃에있는 모든 변경. 아직 아무것도. –

+0

내 목록을 밀어 내고 있던 항목에 대해 wrap_content로 변경해야했습니다. getView()가 호출되지 않으면 getGroupCount와 같은 소리가 호출되지 않거나 0을 반환합니다. –

+0

이 섹션에서는 나에게 맞는 모양이 아니지만 그룹을 사용하는 곳을 볼 수 없습니다. 하위 항목을 추가하고 있지만 그룹을 추가하는 위치 또는 그룹 내의 하위 항목은 어디에 있습니까? \ n this.groups = new ArrayList (); 그룹 그룹 = 새로운 그룹 ("$ 1", "색상"); 자식 하위 = 새 하위 ("$ 2", "빨강"); groups.getChildren(). add (child); this.adapter = new MyExpandableListAdapter (this, groups); –

0

group_list.xml에서 세로 방향을 갖도록 LinearLayout을 변경하는 방법은 무엇입니까?

ExpandableListActivity 문서에 나와있는 레이아웃과 일치하지 않습니다.

getView() 메소드와 같은 위치에 중단 점을 설정하고 전혀 호출되지 않는지 확인할 수도 있습니다.

+0

아직 내 레이아웃을 편집하지 않았습니다. 중단 점도 절대로 트리거되지 않습니다. :( –