3

I'm trying to write a simple example which pulls external data over the network and populates an ExpandableListView via a ExpandableListAdapter. I've tried a couple examples and am pretty much lost. I'm familiar with working with the ArrayAdapter class, but it seems that ExpandableListAdapter is WAY different. Here is my current application code that is displaying nothing:

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>

I, of course, am seeing "EMPTY! DOOM!" rather than the list item I added in my activity. What am I doing wrong?

Also, it seems that the SimpleExpandableListAdapter is supposed to make things easy, but I totally can't figure out how it works or how to use it. Can anyone help me figure this out?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • Does this answer your question? [How to write custom ExpandableListAdapter](https://stackoverflow.com/questions/5188196/how-to-write-custom-expandablelistadapter) – Nimantha Nov 16 '21 at 05:20

2 Answers2

0

I got bit this week with two items trying to use "fill_parent" at the same time and in the same space. It's possible your list is being shoved to the back or off screen. I see you have a simlar setup in your group_list layout. Hope that helps with the invisible list piece.

R Hughes
  • 640
  • 9
  • 22
  • Changed everything in that layout to `match_parent`. Still nothing. – Naftuli Kay Mar 03 '11 at 23:37
  • I had to change to wrap_content on the item that was pushing my list out of the way. But if getView() is not getting called, sounds like getGroupCount is not being called or is returning 0. – R Hughes Mar 04 '11 at 05:48
  • This section does not look right to me, but I don't see where you are using group. You are adding children items, but where are you adding groups, or the children inside a group? \n this.groups = new ArrayList(); Group group = new Group("$1", "Colors"); Child child = new Child("$2", "Red"); groups.getChildren().add(child); this.adapter = new MyExpandableListAdapter(this, groups); – R Hughes Mar 04 '11 at 15:42
0

What about changing the LinearLayout to have a vertical orientation in your group_list.xml?

It doesn't quite match the layout given in the ExpandableListActivity docs.

You can also set breakpoints in places like your getView() method and see if it's being called at all.

Matthew
  • 44,826
  • 10
  • 98
  • 87