Вот, пожалуйста. «QuestionCategory» - мой простой класс для групповых данных. «SimpleQuestion» для данных элемента. Адаптер получает Context, мой адаптер БД и готовый ArrayList с группами - категориями. Адаптер инициализирует свои элементы ArrayList (а затем в getChild () обновляет его при необходимости - когда изменяется groupPosition). В любом случае, он подходит для моих нужд, но любой может адаптировать его для своих нужд. Наслаждайтесь.
private class QuestionCategory{
public int id;
public String name;
QuestionCategory(int pId, String pName){
this.id = pId;
this.name = pName;
}
}
private class SimpleQuestion extends QuestionCategory{
public int categoryId;
SimpleQuestion(int pCatId, int pId, String pName){
super(pId, pName);
categoryId = pCatId;
}
}
private class QuestionListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private DBAdapter mDB;
private ArrayList<QuestionCategory> mCategoriesArrayList;
private ArrayList<SimpleQuestion> mItemsArrayList;
public QuestionListAdapter(Context pContext, DBAdapter pDb, ArrayList<QuestionCategory> pCategoriesArrayList) {
mContext = pContext;
mDB = pDb;
mCategoriesArrayList = pCategoriesArrayList;
mItemsArrayList = new ArrayList<SimpleQuestion>();
}
@Override
public int getGroupCount() {
return mCategoriesArrayList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
int count = 0;
if(mItemsArrayList.isEmpty() || ((SimpleQuestion)mItemsArrayList.get(0)).categoryId != getGroupId(groupPosition)){
Cursor itemsCursor = mDB.getQuestionsCursor((int)getGroupId(groupPosition));
count = itemsCursor.getCount();
itemsCursor.close();
}
else
count = mItemsArrayList.size();
return count;
}
@Override
public Object getGroup(int groupPosition) {
return mCategoriesArrayList.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return ((QuestionCategory)getGroup(groupPosition)).id;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
int categoryId = (int)getGroupId(groupPosition);
//Check if we are not in our current group now, or the current cached items are wrong - MUST BE RECACHED
if(mItemsArrayList.isEmpty() || ((SimpleQuestion)mItemsArrayList.get(0)).categoryId != categoryId){
Cursor itemsCursor = mDB.getQuestionsCursor((int)getGroupId(groupPosition));
itemsCursor.requery();
mItemsArrayList.clear();
if (itemsCursor.moveToFirst())
do {
int id = itemsCursor.getInt(itemsCursor.getColumnIndex(DBAdapter.COL_ID));
String name = itemsCursor.getString(itemsCursor.getColumnIndex(DBAdapter.COL_TEXT));
SimpleQuestion newItem = new SimpleQuestion(categoryId, id, name);
mItemsArrayList.add(newItem);
} while (itemsCursor.moveToNext());
itemsCursor.close();
}
return mItemsArrayList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return ((SimpleQuestion)(getChild(groupPosition, childPosition))).id;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LinearLayout view;
final QuestionCategory group = (QuestionCategory)getGroup(groupPosition);
String name = group.name;
if (convertView == null) {
view = new LinearLayout(mContext);
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(inflater);
vi.inflate(R.layout.question_list_item, view, true);
} else {
view = (LinearLayout) convertView;
}
TextView textTV = (TextView) view.findViewById(R.id.questionListItemTVText);
textTV.setText(name);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout view;
final SimpleQuestion item = (SimpleQuestion)getChild(groupPosition, childPosition);
String name = item.name;
if (convertView == null) {
view = new LinearLayout(mContext);
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(inflater);
vi.inflate(R.layout.question_list_item, view, true);
} else {
view = (LinearLayout) convertView;
}
TextView textTV = (TextView) view.findViewById(R.id.questionListItemTVText);
textTV.setText(name);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}