我在xml文件中显示员工姓名,并通过Spinner显示。它通过ArrayAdapter显示,但如果我选择任何值的值,我需要显示所选的值to 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();}
答案 0 :(得分:0)
// try this way,hope this will help you....
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Employee employee = employees.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String employee=spin.getSelectedItem.toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});