notifyDataSetChanged не работает в пользовательском BaseExpandableListAdapter - PullRequest
0 голосов
/ 01 марта 2019

Мой ящик навигации имеет ExpandableListView, и я хочу обновить его содержимое во время выполнения.Все работает как положено, кроме notifyDataSetChanged.Когда я звоню notifyDataSetChanged в моем обычай ExpandableListAdapter.Я уже перепробовал все обходные пути, представленные в Интернете, но, похоже, ничего не работает.Вот мой адаптер

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private List<String> mListGroup;
private HashMap<String, List<String>> mListChild;

public ExpandableListAdapter(Context mContext, List<String> mListGroup, HashMap<String, List<String>> mListChild) {
    this.mContext = mContext;
    this.mListGroup = mListGroup;
    this.mListChild = mListChild;
}

@Override
public int getGroupCount() {
    return mListGroup.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return mListChild.get(mListGroup.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return mListGroup.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mListChild.get(mListGroup.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                         ViewGroup parent) {
    String groupTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.nav_list_group_main, null);
    }
    TextView title = convertView.findViewById(R.id.expandable_list_group_main);
    title.setTypeface(null, Typeface.BOLD);
    title.setText(groupTitle);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                         View convertView, ViewGroup parent) {
    String childTitle = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.nav_list_child_main, null);
    }
    TextView title = convertView.findViewById(R.id.expandable_list_child_main);
    title.setText(childTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

А вот фрагмент кода, пытающийся обновить данные.

 private void updatetNavigationMenu() {
    List<String> group = Arrays.asList("a", "b", "c", "d");
    List<String> child = Arrays.asList("1", "2", "3", "4");

    expandableListChild.put(group.get(0), child);
    expandableListChild.put(group.get(1), child);
    expandableListChild.put(group.get(2), child);
    expandableListChild.put(group.get(3), child);
    expandableListGroup = new ArrayList<>(expandableListChild.keySet());

    expandableListAdapter.notifyDataSetChanged();
}

После вызова notifyDataSetChanged mListGroup и mListChild не изменяются.Я пытался очистить их вручную, но это тоже не работает.Спасибо!

1 Ответ

0 голосов
/ 01 марта 2019

Вам нужен метод установки в вашем адаптере, такой как

void setData(List<String> list, HashMap<String, List<String>> child) {
    this.mListGroup = list;
    this.mListChild = child;
    notifyDataSetChanged();
}

И в конце вашего updatetNavigationMenu вызова

expandableListAdapter.setData( group, expandableListGroup) ;

notifyDataSetChanged скажет адаптеру обновить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...