我正在显示来自 xml 文件的员工姓名,并通过 Spinner 显示。它通过 ArrayAdapter 显示,但如果我选择任何一个值,我需要将所选值显示为 toast。但它没有显示。我收到字符串 Cast 错误。
List<Employee> employees = null;
try {
XmlPullParserHandler parser = new XmlPullParserHandler();
InputStream is=getAssets().open("employees.xml");
employees = parser.parse(is);
ArrayAdapter<Employee> adapter =new ArrayAdapter<Employee>(this,android.R.layout.simple_list_item_1, employees);
// listView.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(adapter);
String employee=(String)spin.getSelectedItem();
Toast.makeText(getApplicationContext(), "name is :"+employee, Toast.LENGTH_SHORT).show();
} catch (IOException e) {e.printStackTrace();}