notifyDataSetChanged在自定义BaseExpandableListAdapter中不起作用

问题描述 投票:0回答:1

我的导航抽屉有一个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之后,mListGroupmListChild不会改变。我已尝试手动清除它们,但这也无效。谢谢!

android adapter expandablelistview notifydatasetchanged
1个回答
1
投票

您需要在适配器中使用setter方法

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

并在你的updatetNavigationMenu电话结束

expandableListAdapter.setData( group, expandableListGroup) ;

notifyDataSetChanged将告诉适配器刷新。

© www.soinside.com 2019 - 2024. All rights reserved.