Android notifyDataSetChanged在自定义BaseExpandableListAdapter中不工作
我的导航抽屉有一个Android notifyDataSetChanged在自定义BaseExpandableListAdapter中不工作,android,adapter,expandablelistview,notifydatasetchanged,Android,Adapter,Expandablelistview,Notifydatasetchanged,我的导航抽屉有一个ExpandableListView,我想在运行时更新它的内容。除了notifyDataSetChanged之外,一切都正常工作。当我在自定义ExpandableListAdapter中调用notifyDataSetChanged时。我已经尝试了互联网上的所有解决方法,但似乎没有任何效果。 这是我的适配器 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context
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;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私有列表组;
私有HashMap-mListChild;
公共ExpandableListAdapter(上下文mContext、列表mListGroup、HashMap mListChild){
this.mContext=mContext;
this.mListGroup=mListGroup;
this.mListChild=mListChild;
}
@凌驾
public int getGroupCount(){
返回mListGroup.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回mListChild.get(mListGroup.get(groupPosition)).size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回mListGroup.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回mListChild.get(mListGroup.get(groupPosition)).get(childPosition);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getGroupView(int-groupPosition、布尔值isExpanded、视图convertView、,
视图组(父级){
字符串groupTitle=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.nav\u list\u group\u main,空);
}
TextView title=convertView.findViewById(R.id.expandable\u list\u group\u main);
title.setTypeface(null,Typeface.BOLD);
title.setText(groupTitle);
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、boolean-isLastChild、,
视图(视图、视图组父级){
String childTitle=(String)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.nav\u列表\u子\u主,空);
}
TextView title=convertView.findViewById(R.id.expandable\u list\u child\u main);
title.setText(childTitle);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回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();
}
private void updateNavigationMenu(){
列表组=数组;
List 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=newArrayList(expandableListChild.keySet());
expandableListAdapter.notifyDataSetChanged();
}
调用notifydatasetchange
后,mListGroup
和mListChild
不会更改。我尝试过手动清除它们,但也不起作用。
谢谢 在适配器中需要setter方法,如
void setData(List<String> list, HashMap<String, List<String>> child) {
this.mListGroup = list;
this.mListChild = child;
notifyDataSetChanged();
}
notifyDataSetChanged
将通知适配器刷新
expandableListAdapter.setData( group, expandableListGroup) ;