首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >notifyDataSetChanged在自定义BaseExpandableListAdapter中不起作用

notifyDataSetChanged在自定义BaseExpandableListAdapter中不起作用
EN

Stack Overflow用户
提问于 2019-03-01 05:12:36
回答 1查看 100关注 0票数 0

我的导航抽屉有一个ExpandableListView,我想在运行时更新它的内容。除了notifyDataSetChanged之外,一切都像预期的那样工作。当我在自定义ExpandableListAdapter中调用notifyDataSetChanged时。我已经尝试了互联网上的所有变通方法,但似乎都不起作用。这是我的适配器

代码语言:javascript
复制
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;
}
}

这是试图更新数据的代码片段。

代码语言:javascript
复制
 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没有改变。我尝试过手动清除它们,但也不起作用。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-01 06:52:32

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

代码语言:javascript
复制
void setData(List<String> list, HashMap<String, List<String>> child) {
    this.mListGroup = list;
    this.mListChild = child;
    notifyDataSetChanged();
}

在您的updatetNavigationMenu调用结束时

代码语言:javascript
复制
expandableListAdapter.setData( group, expandableListGroup) ;

notifyDataSetChanged将通知适配器刷新。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54934336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档