ExpandableListView没有以正确的顺序出现

时间:2016-08-28 23:18:56

标签: java android hashmap expandablelistview

我正在编写一个Android应用程序,我决定使用可扩展列表视图。我有我需要的一切,列表工作正常,但是当我填充它时,组看起来与它们应该的顺序不同,这里是一个例子: Unexpanded

Expanded

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

我的片段代码:

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并使用它的排序功能(作为最后的手段)吗?谢谢你的帮助。

P.S。在做我的ExpandableListView时,我正在学习本教程(我不知道这是否重要):Tutorial

2 个答案:

答案 0 :(得分:0)

根据HashMap的Java规范,订单无法保证:

  

......此课程不保证地图的顺序;特别是,它不保证订单会随着时间的推移保持不变......

因此,如果您想保证组的顺序,则必须使用不同的数据结构。

有关类似的讨论,请参阅this answer,以及维护广告订单顺序的LinkedHashMap的建议用法。

答案 1 :(得分:0)

这是因为HashMap在插入项目时不会保持其顺序。因此,您代码中的所有位置(活动和适配器)都将HashMap更改为LinkedHashmap。喜欢

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