tags:

views:

256

answers:

2

I have an ExpandableListView in which I'd like to have controls other than TextView. Apparently, SimpleExandableListViewAdapter assumes all the controls are TextViews. A cast exception is generated if they are not.

What is the recommended solution?

Options I can think of include: - Use some other included adapter. But I can't tell if they all have the same assumption. - Create my own adapter. Is there a doc which describes the contract, ie the sequence of method calls an adapter will encounter?

I expected the existing adapters to require the views to conform to some interface to allow any conforming view to be used, rather than hardcode to textview and limit where they can be used.

A: 

BaseExpandableListAdapter

This works well if you follow the example from here: http://developer.android.com/intl/zh-CN/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html and you use a custom view build or inflated by code.

copy paste from my code:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            return viewContacts.inflateRow(null, viewContacts.this, getChild(groupPosition,
                    childPosition));

        }
Pentium10
yep, extend BaseExpadableListAdapter and then override all of its methods. pretty straight forward except one has to deal with groupViews and childViews. As far as views other than TextViews, look into LayoutInflater docs http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Raja
A: 

Here is some code that I actually just wrote for one of my apps:

public class ExpandableAppAdapter extends BaseExpandableListAdapter
{
    private PackageManager m_pkgMgr;
    private Context m_context;
    private List<ApplicationInfo> m_groups;
    private List<List<ComponentName>> m_children;

    public ExpandableAppAdapter(Context context, List<ApplicationInfo> groups, List<List<ComponentName>> children)
    {
        m_context = context;
        m_pkgMgr = m_context.getPackageManager();
        m_groups = groups;
        m_children = children;
    }

    @Override
    public Object getChild(int groupPos, int childPos) 
    {
        return m_children.get(groupPos).get(childPos);
    }

    @Override
    public long getChildId(int groupPos, int childPos) 
    {
        return childPos;
    }

    @Override
    public int getChildrenCount(int groupPos) 
    {
        return m_children.get(groupPos).size();
    }

    @Override
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) 
    {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(m_context);
            convertView = inflater.inflate(R.layout.expandable_app_child_row, null);
        }

        ComponentName child = (ComponentName)getChild(groupPos, childPos);
        TextView txtView = (TextView)convertView.findViewById(R.id.item_app_pkg_name_id);
        if (txtView != null)
            txtView.setText(child.getPackageName());

        txtView = (TextView)convertView.findViewById(R.id.item_app_class_name_id);
        if (txtView != null)
            txtView.setText(child.getClassName());

        convertView.setFocusableInTouchMode(true);
        return convertView;
    }

    @Override
    public Object getGroup(int groupPos) 
    {
        return m_groups.get(groupPos);
    }

    @Override
    public int getGroupCount() 
    {
        return m_groups.size();
    }

    @Override
    public long getGroupId(int groupPos) 
    {
        return groupPos;
    }

    @Override
    public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
    {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(m_context);
            convertView = inflater.inflate(R.layout.expandable_app_group_row, null);
        }

        ApplicationInfo group = (ApplicationInfo)getGroup(groupPos);
        ImageView imgView = (ImageView)convertView.findViewById(R.id.item_selection_icon_id);
        if (imgView != null)
        {
            Drawable img = m_pkgMgr.getApplicationIcon(group);
            imgView.setImageDrawable(img);
            imgView.setMaxWidth(20);
            imgView.setMaxHeight(20);
        }

        TextView txtView = (TextView)convertView.findViewById(R.id.item_app_name_id);
        if (txtView != null)
            txtView.setText(m_pkgMgr.getApplicationLabel(group));

        return convertView;
    }
    @Override
    public boolean hasStableIds() 
    {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPos, int childPos) 
    {
        return true;
    }
}

It took me a little longer than I would have liked to figure it out... but it really isn't as bad as I thought it was going to be. Hope that helps! There is an example of LayoutInflater in the code as well.

Justin