gpt4 book ai didi

android - 可扩展 ListView 顺序不正确

转载 作者:行者123 更新时间:2023-11-29 19:38:58 30 4
gpt4 key购买 nike

我的可扩展 ListView 的顺序似乎不正确。

这是我的数据提供者:

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... 知道为什么吗?谢谢!

最佳答案

这很可能是因为 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);

关于android - 可扩展 ListView 顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859893/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com