Android日程程序开发问题:选中Spinner班级后如何显示对应日程?
实现班级选择后显示对应日程的解决方案
问题分析
你当前的代码已经实现了根据专业选择动态切换班级列表的功能,但缺少子Spinner(班级选择)的选择事件监听,因此无法在选中班级时更新日程文本。同时,你需要建立班级与对应日程的映射关系,才能根据选中的班级获取对应的日程内容。
具体实现步骤
1. 建立班级与日程的映射关系
首先创建一个HashMap来存储每个班级对应的日程信息,将班级名称作为键,日程文本作为值。示例如下:
// 全局或Activity内定义映射表 HashMap<String, String> classScheduleMap = new HashMap<>(); // 在初始化代码中填充映射(根据你的实际班级和日程修改) // 例如:会计专业的班级日程 classScheduleMap.put("Облік 1 група", "周一: 财务会计\n周二: 税法基础\n周三: 统计学"); classScheduleMap.put("Облік 2 група", "周一: 管理会计\n周二: 审计学\n周三: 经济法"); // 其他专业班级同理添加
2. 移除父Spinner中无效的代码
在sp_parent的onItemSelected方法中,你尝试直接获取sp_child的选中项并设置到TextView,这是无效的(因为此时子Spinner刚设置适配器,还未完成选择)。需要删除这些错误代码:
// 删除以下两行(在position==0和position==1中的代码) String selected = (String) sp_child.getItemAtPosition(position); schedule_text.setText(selected);
3. 为子Spinner添加选择监听器
给sp_child设置OnItemSelectedListener,当选中班级时从映射表中获取对应的日程并更新TextView:
sp_child.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { // 获取选中的班级名称 String selectedClass = (String) adapterView.getItemAtPosition(position); // 从映射表中获取对应的日程 String schedule = classScheduleMap.get(selectedClass); // 更新TextView显示 if (schedule != null) { schedule_text.setText(schedule); } else { schedule_text.setText("暂无该班级的日程信息"); } } @Override public void onNothingSelected(AdapterView<?> adapterView) { schedule_text.setText("请选择班级"); } });
4. 优化父Spinner的代码(可选)
使用switch-case替代多个if判断,让代码更清晰:
sp_parent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { switch (position) { case 0: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_oblic); break; case 1: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_food_technology); break; case 2: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_hotel); break; case 3: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_law); break; case 4: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_comp_genius); break; case 5: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_businessman); break; case 6: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_tourism); break; case 7: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_economy); break; } sp_child.setAdapter(arrayAdapter_child); } @Override public void onNothingSelected(AdapterView<?> adapterView) {} });
完整代码示例(关键部分)
// 初始化映射表 HashMap<String, String> classScheduleMap = new HashMap<>(); classScheduleMap.put("Облік 1 група", "周一: 财务会计\n周二: 税法基础\n周三: 统计学"); classScheduleMap.put("Облік 2 група", "周一: 管理会计\n周二: 审计学\n周三: 经济法"); // 其他班级日程... // 父Spinner设置 sp_parent = findViewById(R.id.spinner); sp_child = findViewById(R.id.spinner3); schedule_text = findViewById(R.id.your_textview_id); // 确保你初始化了这个TextView // 专业列表初始化 ArrayList<String> arrayList_parent = new ArrayList<>(); arrayList_parent.add("Облік і оподаткування"); arrayList_parent.add("Харчові технології"); arrayList_parent.add("Готельно-ресторанна справа"); arrayList_parent.add("Право"); arrayList_parent.add("Комп'ютерна інженерія"); arrayList_parent.add("Підприємництво, торгівля та біржова діяльність"); arrayList_parent.add("Туризм"); arrayList_parent.add("Фінанси, банківська справа і страхування"); ArrayAdapter<String> arrayAdapter_parent = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_parent); sp_parent.setAdapter(arrayAdapter_parent); // 父Spinner选择监听 sp_parent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { switch (position) { case 0: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_oblic); break; case 1: arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_food_technology); break; // 其他case... } sp_child.setAdapter(arrayAdapter_child); } @Override public void onNothingSelected(AdapterView<?> adapterView) {} }); // 子Spinner选择监听 sp_child.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { String selectedClass = (String) adapterView.getItemAtPosition(position); String schedule = classScheduleMap.get(selectedClass); schedule_text.setText(schedule != null ? schedule : "暂无日程信息"); } @Override public void onNothingSelected(AdapterView<?> adapterView) { schedule_text.setText("请选择班级"); } });
内容的提问来源于stack exchange,提问作者Wave Miami
相关产品推荐
相关产品推荐

