I've been trying to figure this one out for a while now, but no matter how much I look, I haven't found an answer. So far I've checked the following questions: Set selected item of spinner programmatically, How to set selected item of Spinner by value, not by position?, and How to set position in spinner?. So far none of them have worked for me. I'm trying to do this on Android 4.4.2, and here's my code:
String dataReturned11 = someData.getString("unitCheckFour", "degrees F");
if (dataReturned11 == ("degrees C")) {
tempUnits.setSelection(((ArrayAdapter<String>)tempUnits.getAdapter()).getPosition("degrees C"));
} else {
tempUnits.setSelection(((ArrayAdapter<String>)tempUnits.getAdapter()).getPosition("degrees F"));
};
What I'm trying to do is set it based on a value that was saved earlier, and it doesn't seem to be working. An important thing to note is that this gave me an unchecked cast warning, so I suppressed it. It does not seem to have had any effect on my problem whatsoever.
EDIT: Since it was asked for, here is the code behind the spinner itself:
tempUnits = (Spinner) findViewById(R.id.tempUnits);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,
R.array.tempUnits, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tempUnits.setAdapter(adapter4);
I hope this helps.