java - ExpandableListView 未按正确顺序出现

标签 java android hashmap expandablelistview

我正在编写一个 Android 应用程序,我决定使用可扩展 ListView 。我拥有所需的一切,列表工作正常,但是当我填充它时,组的顺序似乎与应有的顺序不同,这是一个示例: Unexpanded

Expanded

如您所见,子值很好,只有父值以随机顺序出现。这是我的完整代码:

我的 fragment 代码:

public class BuildingsFragment extends Fragment {


public BuildingsFragment() {
    // Required empty public constructor
}


public static BuildingsFragment newInstance(Context context) {
    BuildingsFragment fragment = new BuildingsFragment();

    return fragment;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_buildings, container, false);


    final ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListViewBuildings);
    HashMap<String, List<String>> expandableListDetail = ExpandableListDataPump.getData();
    List<String> expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    ExpandableListAdapter expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail);
    expandableListView.setAdapter(expandableListAdapter);


    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


            Fragment fragment = null;
            Class fragmentClass = BuildingDetailsFragment.class;



            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }

            FragmentTransaction trans = getFragmentManager().beginTransaction();


            Bundle args = new Bundle();
            args.putString("BUILDING_NAME", "test");
            fragment.setArguments(args);


            trans.replace(R.id.frameLayoutForFragments, fragment);


            trans.addToBackStack(null);


            trans.commit();


            return false;
        }
    });


    return view;
}
}

我的自定义适配器:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_view_buildings_row_layout, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_view_buildings_group_layout, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listGroup);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}
}

我正在使用的 ExpandableListDataPump:

public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {

    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();


    List<String> abuildings = new ArrayList<String>();
    abuildings.add("A-1");
    abuildings.add("A-2");
    abuildings.add("A-3");
    abuildings.add("A-5");
    abuildings.add("A-7");
    expandableListDetail.put("A", abuildings);

    List<String> bbuildings = new ArrayList<String>();
    bbuildings.add("B-1");
    bbuildings.add("B-2");
    bbuildings.add("B-3");
    bbuildings.add("B-5");
    bbuildings.add("B-6");
    expandableListDetail.put("B", bbuildings);

    List<String> cbuildings = new ArrayList<String>();
    cbuildings.add("C-1");
    cbuildings.add("C-3");
    cbuildings.add("C-4");
    cbuildings.add("C-5");
    cbuildings.add("C-6");
    expandableListDetail.put("C", cbuildings);

    List<String> dbuildings = new ArrayList<String>();
    dbuildings.add("D-1");
    dbuildings.add("D-2");
    dbuildings.add("D-3");
    dbuildings.add("D-5");
    dbuildings.add("D-7");
    expandableListDetail.put("D", dbuildings);

    return expandableListDetail;
}
}

我认为这就是与 ExpandableListView 相关的所有内容。我真的很想离开 HashMap,但如果没有其他办法,我可以将其更改为 TreeMap 并使用它的排序功能(作为最后的手段)吗?感谢您的帮助。

附注在执行 ExpandableListView 时,我正在遵循本教程(我不知道这是否重要):Tutorial

最佳答案

这是因为 HashMap 不维护插入项的顺序。因此,在代码(Activity 和 Adapter)中的任何地方,将 HashMap 更改为 LinkedHashmap。喜欢

  private LinkedHashMap<String, List<String>> expandableListDetail;

关于java - ExpandableListView 未按正确顺序出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196655/

相关文章:

java - 没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session

java - Log4j2 配置 - 找不到 log4j2 配置文件

android - 有没有一种方法可以使用游标适配器来显示其项目由 sqlite 支持的列表

java - Android-从字符串中删除 URL 百分号

data-structures - 如何在 Lua 中创建 HashMap<Int, Int[]> 的等效项

java - 如何将 ServerSocket 绑定(bind)到 InetAddress?

java - Mapreduce - 排序作业?

java - Android 构建工作区错误

java - 如何获取 HashMap 的重复键值对?

java - 使用字符串和列表作为参数迭代 HashMap