Skip to content Skip to sidebar Skip to footer

What Adapter To Use For Expandablelistview With Non-textview Views?

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 excep

Solution 1:

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

publicclassExpandableAppAdapterextendsBaseExpandableListAdapter
{
    private PackageManager m_pkgMgr;
    private Context m_context;
    private List<ApplicationInfo> m_groups;
    private List<List<ComponentName>> m_children;

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

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

    @OverridepubliclonggetChildId(int groupPos, int childPos) 
    {
        return childPos;
    }

    @OverridepublicintgetChildrenCount(int groupPos) 
    {
        return m_children.get(groupPos).size();
    }

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

        ComponentNamechild= (ComponentName)getChild(groupPos, childPos);
        TextViewtxtView= (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;
    }

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

    @OverridepublicintgetGroupCount() 
    {
        return m_groups.size();
    }

    @OverridepubliclonggetGroupId(int groupPos) 
    {
        return groupPos;
    }

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

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

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

        return convertView;
    }
    @OverridepublicbooleanhasStableIds() 
    {
        returnfalse;
    }
    @OverridepublicbooleanisChildSelectable(int groupPos, int childPos) 
    {
        returntrue;
    }
}

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.

Solution 2:

yup you can just have the getChildView and getGroupView methods return any custom view you want.

I have a blog post that covers ExpandableListView in lots of detail here: http://qtcstation.com/2011/03/working-with-the-expandablelistview-part-1/

Solution 3:

BaseExpandableListAdapter

This works well if you follow the example from here: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.java 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));

        }

Post a Comment for "What Adapter To Use For Expandablelistview With Non-textview Views?"