My preset values for the spinner comes from a String Array.
<string-array name="currencyArray">
<item>INR</item>
<item>USD</item>
<item>EUR</item>
</string-array>
I am able to display this and save this fine. I am adding some values in a SQLite DB on "Add". On update, i want to fetch the values and display on screen. One of them is the spinner.
I tried this code
String myString = getIntent().getStringExtra("Currency");
System.out.println(myString);
ArrayAdapter myAdap = (ArrayAdapter) currencySpin.getAdapter();
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
currencySpin.setSelection(spinnerPosition);
The sysout (line 2) actually displays the correct value from the DB. But the spinner still displays the initial value and not the DB value. What am i missing here?
I am using this code to populate and read the spinner in an empty screen
// reading the spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.currencyArray,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
currencySpin.setAdapter(adapter);
currencySpin.setOnItemSelectedListener(this);
Updated code
String myString = getIntent().getStringExtra("Currency");
System.out.println(myString);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.currencyArray,
android.R.layout.simple_spinner_item);
spinnerPosition = adapter.getPosition(myString);
System.out.println(spinnerPosition);
//set the default according to value
currencySpin.setSelection(spinnerPosition);
The spinnerPosition is coming as the correct number (changes from 0 to 2 as per my data), but the currencySpin (my spinner) does not display it correctly. What am i missing??