可扩展列表视图的顺序不正确

时间:2016-08-09 20:31:55

标签: android expandablelistview

我的可扩展列表视图似乎没有正确的顺序。

这是我的数据提供者:

public class DataProvider {

public static HashMap<String, List<String>> getInfo() {

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

    List<String> Workout_29 = new ArrayList<String>();
    Workout_29.add("Week One");
    Workout_29.add("Week Two");
    Workout_29.add("Week Three");
    Workout_29.add("Week Four");

    List<String> Workout_30 = new ArrayList<String>();
    Workout_30.add("Week One");
    Workout_30.add("Week Two");
    Workout_30.add("Week Three");
    Workout_30.add("Week Four");

    List<String> Workout_31 = new ArrayList<String>();
    Workout_31.add("Week One");
    Workout_31.add("Week Two");
    Workout_31.add("Week Three");
    Workout_31.add("Week Four");

    List<String> Workout_32 = new ArrayList<String>();
    Workout_32.add("Week One");
    Workout_32.add("Week Two");
    Workout_32.add("Week Three");
    Workout_32.add("Week Four");

    List<String> Workout_37 = new ArrayList<String>();
    Workout_37.add("Week One");
    Workout_37.add("Week Two");
    Workout_37.add("Week Three");
    Workout_37.add("Week Four");

    WorkoutDetails.put("Workout 29", Workout_29);
    WorkoutDetails.put("Workout 30", Workout_30);
    WorkoutDetails.put("Workout 31", Workout_31);
    WorkoutDetails.put("Workout 32", Workout_32);
    WorkoutDetails.put("Workout 37", Workout_37);

    return WorkoutDetails;
}

}

这是创建它的地方:

public class MyWorkout extends BaseActivity{

ExpandableListView expandableListView;
HashMap<String, List<String>> Workouts_details;
List<String> Workout_list;
WorkoutsAdapter mAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_workout);
    mToolBar = activateToolbar();
    setUpNavigationDrawer();
    getSupportActionBar().setTitle("My Workout");


    expandableListView = (ExpandableListView) findViewById(R.id.listViewWorkouts);
    Workouts_details = DataProvider.getInfo();
    Workout_list = new ArrayList<>(Workouts_details.keySet());
    mAdapter = new WorkoutsAdapter(this, Workouts_details, Workout_list);



    expandableListView.setAdapter(mAdapter);

你可以看到它应该是29,30,31,32,37 ..但实际上它是29,30,32,37,31 ......任何想法为什么?谢谢!

2 个答案:

答案 0 :(得分:1)

这很可能是因为HashMap无法保证返回的键集中键的顺序。我的猜测是,如果您随后对您在此构建的列表进行排序,您将获得所需的排序:

    Workout_list = new ArrayList<>(Workouts_details.keySet());
    // Now sort Workout_list...
    // As it is simply a list of strings, we can sort it according to its natural ordering (lexicographical) to get your desired result like so:
    java.util.Collections.sort(Workout_list);

答案 1 :(得分:0)

您可以将HashMap 更改为LinkedHashmap。就这样

LinkedHashMap<String, List<String>> WorkoutDetails = new LinkedHashMap<String, List<String>>();