0

我正在显示来自 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();}
4

2 回答 2

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) {

        }
 });
于 2014-05-09T11:57:54.837 回答
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) {

            }
});
于 2014-05-09T11:42:52.553 回答