Я использую расширяемый список для заполнения данных категорий.У немногих категорий есть подкатегории, а у некоторых нет.Список родительских категорий отображается отлично.Проблема заключается в подкатегориях.Добавленные данные верны, но при рендеринге данных в адаптере подкатегории в groupId 1 добавляются в другие groupId, у которых есть подкатегории, из-за которых подкатегории groupId отображаются в других категориях.
Приведенный ниже код работаеткогда данные установлены в HashMap.
private List<String> listDataHeader = new ArrayList<>();
private HashMap<String, List<String>> listDataChild = new
HashMap<String, List<String>>();
private List<String> childValues = new ArrayList<>();
@Override
public void categoriesFetched(List<ArticleCategory> categories) {
listDataHeader.clear();
listDataChild.clear();
childValues.clear();
for (int i = 0; i < categories.size(); i++) {
if (categories.get(i).getSub_categories().size() > 0) {
childValues = new ArrayList<>();
for (int j = 0; j < categories.get(i).getSub_categories().size(); j++) {
childValues.add(categories.get(i).getSub_categories().get(j).getValue());
}
listDataChild.put(categories.get(i).getValue(), childValues);
} else {
listDataChild.put(categories.get(i).getValue(), new ArrayList<String>());
}
listDataHeader.add(categories.get(i).getValue());
}
articlesCategoryAdapter.notifyDataSetChanged();
}
Приведенный ниже фрагмент кода является классом адаптера при отображении представления.
public class ArticlesCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader = new ArrayList<>();
private HashMap<String, List<String>> _listDataChild = new HashMap<>();
public ArticlesCategoryAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_child, null);
String childText = (String) getChild(groupPosition, childPosition);
AppCompatTextView txtListChild = (AppCompatTextView) convertView.findViewById(R.id.txtSubCategoryName);
txtListChild.setText(childText);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_category_group, null);
AppCompatTextView lblListHeader = (AppCompatTextView) convertView.findViewById(R.id.txtMainCategoryName);
lblListHeader.setText(headerTitle);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}