Tutorial :What adapter to use for ExpandableListView with non-TextView views?



Question:

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.


Solution:1

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.


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));            }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »