I have three spinners in an activity where I can edit information that the user has already entered into a database. I have been able to set the text of a textfield by querying my SQLite database to match what the user can edit. Can the same be done with a spinner?
For example, if a user picked the year 2000 previously, in the new edit activity I want the spinner to be already selected at 2000 and then they can change this if they want to.
So far I have only seen options to set positions of spinners based on an integer position. Can this be done by providing a year? Thanks for any help.
Here's what I am currently working on with my month spinner:
ArrayList<String> list = new ArrayList( Arrays.asList(getResources().getStringArray(R.array.months)) );
int pos = list.get("March");
spinMonth.setSelection(pos);
All you can do is identifying the position of the item in the spinner and the setting the selection of spinner.
int pos=yourlist.indexOf("2000");
spinner.setSelection(pos);
Edit:
ArrayList<String> list=new ArrayList( Arrays.asList(getResources().getStringArray(R.array.years)) ); // your array id of string resource
int pos= list.indexOf("2000");
spinner.setSelection(pos);
I had to do the exact same thing the other week. Here was my approach.
String userName = dataCursor.getString(dataCursor.getColumnIndex("USER_NAME"));
int position = listOfUsers.indexOf(userName);
userSpinner.setSelection(position);
where listOfUsers
is the list I use to popular the Spinner, userSpinner
. Hope that helps!
简单的一行代码是
spinMonth.setSelection(Arrays.asList(getResources().getStringArray(R.array.months)).indexOf("March"));
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.