首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >似乎找不出ExpandableListAdapter

似乎找不出ExpandableListAdapter
EN

Stack Overflow用户
提问于 2011-03-03 22:43:16
回答 2查看 6.9K关注 0票数 3

我正在尝试编写一个简单的示例,它通过网络提取外部数据,并通过一个ExpandableListView填充一个ExpandableListAdapter。我试过几个例子,但我几乎迷失了方向。我很熟悉使用ArrayAdapter类,但是ExpandableListAdapter似乎是完全不同的。下面是我当前的应用程序代码,它没有显示任何内容:

MyExpandableListAdapter

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

当然,我看到的是“空的!毁灭的!”而不是我在活动中添加的列表项。我做错了什么?

而且,似乎SimpleExpandableListAdapter应该使事情变得简单,但我完全搞不清楚它是如何工作的,也不知道如何使用它。有人能帮我弄清楚吗?

EN

回答 2

Stack Overflow用户

发布于 2011-03-03 22:53:36

本周,我在同一时间和同一空间尝试使用"fill_parent“的两个项目被咬了。这可能是你的名单是被推到后面或屏幕外。我看到你在你的group_list布局中有一个简单的设置。希望这能帮上看不见的名单。

票数 0
EN

Stack Overflow用户

发布于 2011-03-03 22:58:27

如何将LinearLayout更改为在group_list.xml中具有垂直方向?

它与ExpandableListActivity文档中给出的布局不完全匹配。

您还可以在getView()方法等地方设置断点,并查看是否正在调用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5187551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档