我想寫一個簡單的例子,通過網絡拉動外部數據,並通過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>
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>
我,當然,我看到「空的! DOOM!「而不是我在我的活動中添加的列表項目。我在做什麼錯了?
此外,SimpleExpandableListAdapter
似乎應該使事情變得簡單,但我完全無法弄清楚它是如何工作的或如何使用它。任何人都可以幫我弄明白這一點嗎?
已將該佈局中的所有內容更改爲「match_parent」。依然沒有。 – 2011-03-03 23:37:33
我不得不改變wrap_content項目,推動我的列表的方式。但是,如果getView()沒有被調用,聽起來像getGroupCount沒有被調用或返回0. – 2011-03-04 05:48:38
這部分看起來不正確,但我沒有看到你在哪裏使用組。您正在添加兒童項目,但您要添加小組,還是小組內的孩子? \ n this.groups = new ArrayList();羣組=新羣組(「$ 1」,「顏色」);兒童孩子=新孩子(「$ 2」,「紅」); 。groups.getChildren()添加(子); this.adapter = new MyExpandableListAdapter(this,groups); –
2011-03-04 15:42:00