【问题标题】:notifyDataSetChanged not working in custom BaseExpandableListAdapternotifyDataSetChanged 在自定义 BaseExpandableListAdapter 中不起作用
【发布时间】:2019-07-22 21:05:21
【问题描述】:

我的导航抽屉有一个ExpandableListView,我想在运行时更新它的内容。除了notifyDataSetChanged,一切都按预期工作。当我在自定义ExpandableListAdapter 中调用notifyDataSetChanged 时。我已经尝试了互联网上的所有解决方法,但似乎没有任何效果。 这是我的适配器

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;
}
}

这是试图更新数据的 sn-p。

 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();
}

调用notifyDataSetChangedmListGroupmListChild 不会改变。我试过手动清除它们,但这也不起作用。 谢谢!

【问题讨论】:

    标签: android adapter expandablelistview notifydatasetchanged


    【解决方案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 会告诉适配器刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      相关资源
      最近更新 更多