java - 使用 Spinner 打开另一个类
问题描述
尝试使用此代码从微调器的下拉列表中打开一个类。因此,如果按下帮助,则会打开帮助活动,如果按下导航,则会打开导航类
//declare spinner.
Spinner dropdown = findViewById(R.id.spinner1);
String[] items = new String[]{"Help", "Navigation Help"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
解决方案
由于您使用了 findViewById(...) ,我假设您在 Activity 中设置 onItemSelectedListener 。
Context context = this;
HashMap<String , Class> hashMap = new HashMap<>();
hashMap.put("Naviagtion" , NavigationActivity.class);
hashMap.put("Help" ,HelpActivity.class);
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String string = adapter.getItem(pos);
Intent intent = new Intent(context , hashMap.get(string));
startActivity(intent);
}
推荐阅读
- android - AsyncListDiffer 实现。mReadOnlyList 如何工作?
- java - 多线程 JaxRs 调用——parallelStream 还是 Futures?
- arrays - VBA用'OR'评估'IF'返回错误值
- vba - Excel VBA 打开超链接 - 最初很慢?
- c# - 是否有比使用性能计数器查找进程的 CPU 使用率和内存使用率更快的替代方法?
- python - 如何在 Python 中以特定方式重新组织列表
- javascript - Firebase 云消息传递无法读取未定义的属性“receiver_id”
- php - 如何通过 id -jquery 获取动态值
- javascript - 在此代码中定义回调的位置
- vba - 用于将数据导出到不同服务器的代码中的 VBA Excel 对象定义错误