I\'m looking to write my own ExpandableListAdapter which operates similarly to ArrayAdapter. My data model is this:
public class Gr
Seeing how old this post and answers are, I thought I would point out there is a very nice 3rd party library that sorta fills in this missing gap. While the posted custom solutions are good, they are still missing some things and are following the cumbersome design of requiring the programmer to generate a data structure of data structures. Sometimes,you just want to organize one List into nice little groups without the hassle of doing it yourself.
It's called the RolodexArrayAdapter and can be easily utilized in creating custom ExpandableListAdapters...without having to worry about all the data management problems and features. It supports methods like add, addAll, remove, removeAll, retainAll, contains, sorting etc. It also supports more advanced features like ChoiceMode, Filtering, and auto expanding groups.
Example:
class MovieAdapter extends RolodexArrayAdapter {
public MovieAdapter(Context activity, List movies) {
super(activity, movies);
}
@Override
public Integer createGroupFor(MovieItem childItem) {
//Lets organize our movies by their release year
return childItem.year;
}
@Override
public View getChildView(LayoutInflater inflater, int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
//Inflate your view
}
//Fill view with data
return convertView;
}
@Override
public View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
//Inflate your view
}
//Fill view with data
return convertView;
}
@Override
public boolean hasAutoExpandingGroups() {
return true;
}
@Override
protected boolean isChildFilteredOut(MovieItem movie, CharSequence constraint) {
//Lets filter by movie title
return !movie.title.toLowerCase(Locale.US).contains(
constraint.toString().toLowerCase(Locale.US));
}
@Override
protected boolean isGroupFilteredOut(Integer year, CharSequence constraint) {
//Lets filter out everything whose year does not match the numeric values in the constraint.
return TextUtils.isDigitsOnly(constraint) && !year.toString().contains(constraint);
}
}