Java Android ExpandableListView随机显示项目

Java Android ExpandableListView随机显示项目,java,android,Java,Android,所以我有一个移动应用程序,它从文件系统加载几个对象,然后按ID对它们进行排序,然后将它们作为类别添加到可扩展的ListView中。但是,尽管在Object[]数组中项目仍然正确排序,但ExpandableListView似乎随机更改顺序。 在此情况下,事件的顺序应如下所示: 事件ID:4 事件4事件ID:3 事件3事件ID:2 事件2事件ID:1 事件1事件ID:0 数组排序正确,但添加到ExpandableListView时,顺序是随机的 以下是我的EventsListViewAdapter的

所以我有一个移动应用程序,它从文件系统加载几个对象,然后按ID对它们进行排序,然后将它们作为类别添加到可扩展的ListView中。但是,尽管在Object[]数组中项目仍然正确排序,但ExpandableListView似乎随机更改顺序。 在此情况下,事件的顺序应如下所示:

事件ID:4
事件4事件ID:3
事件3事件ID:2
事件2事件ID:1
事件1事件ID:0

数组排序正确,但添加到ExpandableListView时,顺序是随机的

以下是我的EventsListViewAdapter的代码:

public class EventsAdapter extends BaseExpandableListAdapter {

private Context context;
private HashMap<String, List<String>> Events_Category;
private List<String> Events_List;

public EventsAdapter(Context context, HashMap<String, List<String>> Movies_Category, List<String> Movies_List) {
    this.context = context;
    this.Events_Category = Movies_Category;
    this.Events_List = Movies_List;
}

@Override
public int getGroupCount() {
    return Events_List.size();
}
@Override
public int getChildrenCount(int groupPosition) {
    return Events_Category.get(Events_List.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
    return Events_List.get(groupPosition);
}
@Override
public Object getChild(int parentPosition, int childPosition) {
    return Events_Category.get(Events_List.get(parentPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}
@Override
public boolean hasStableIds() {
    return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String group_title = (String) getGroup(groupPosition);
    if(convertView == null) {
        LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(com.cpjd.roblu.R.layout.parent_layout, parent, false);
    }
    TextView parent_textview = (TextView) convertView.findViewById(com.cpjd.roblu.R.id.parent_txt);
    parent_textview.setTypeface(null, Typeface.BOLD);

    if(Text.getAPI() > 21) {
        parent_textview.setTextColor(Color.parseColor("#FFFFFF"));
    } else {
        parent_textview.setTextColor(Color.parseColor("#2C3539"));
    }
    parent_textview.setText(group_title);
    return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView) {
    String child_title = (String)getChild(groupPosition, childPosition);
    if(convertView == null) {
        LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(com.cpjd.roblu.R.layout.child_layout, parentView, false);
    }
    TextView child_textview = (TextView) convertView.findViewById(com.cpjd.roblu.R.id.child_txt);
    child_textview.setText(child_title);

    return convertView;

}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
公共类EventsAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有HashMap事件\u类别;
私人列表事件(u列表);;
公共事件适配器(上下文上下文、HashMap电影\u类别、列表电影\u列表){
this.context=上下文;
this.Events\u Category=电影\u Category;
this.Events\u List=电影\u List;
}
@凌驾
public int getGroupCount(){
返回事件_List.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回Events_Category.get(Events_List.get(groupPosition)).size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回事件\u List.get(groupPosition);
}
@凌驾
公共对象getChild(int-parentPosition,int-childPosition){
返回事件\类别.get(事件\列表.get(父位置)).get(子位置);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
String group_title=(String)getGroup(groupPosition);
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(com.cpjd.roblu.R.layout.parent_layout,parent,false);
}
TextView parent_TextView=(TextView)convertView.findViewById(com.cpjd.roblu.R.id.parent_txt);
parent_textview.setTypeface(null,Typeface.BOLD);
if(Text.getAPI()>21){
parent_textview.setTextColor(Color.parseColor(“#FFFFFF”));
}否则{
parent#textview.setTextColor(Color.parseColor(#2C3539”);
}
parent_textview.setText(组标题);
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、boolean-isLastChild、视图转换视图、视图组父视图){
String child_title=(String)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(com.cpjd.roblu.R.layout.child_layout,parentView,false);
}
TextView child_TextView=(TextView)convertView.findViewById(com.cpjd.roblu.R.id.child_txt);
child_textview.setText(child_title);
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}

此问题与此类似。谢谢,这是一篇有用的帖子,我可以使用
LinkedHashMap
解决此问题。只需将所有
HashMap
替换为
LinkedHashMap
,它工作得非常好。谢谢!