简体   繁体   中英

using android java spinner

i'm trying to use a few spinners in android to display a list of choices to the user. The spinners are made in xml but i want to use it from java because depending on the user's choice the text i want displayed in spinner will be different. The data is stored in a standard String array. I've tried many different methods and examples but i couldnt get it to work. The Spinners are assigned in java in a loop.

     units[n] = (Spinner) findViewById (data.Eid[n*3+3]);
 ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.unit, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     units[n].setAdapter(adapter);

i keep getting an error at R.array.unit. i tried eclipse suggested suggestion but it didnt work. Is there a better way to do this? I also cant figure out how to add the data from the array to the spinner and i'd like it to have the first option as the default.

I believe R.array.unit should be defined in your project because it is not part of android.R.array class.

For creating a spinner from an array adapter a good example is provided in the developer guide. See the next link. http://developer.android.com/resources/tutorials/views/hello-spinner.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM