0

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??

Jasma
  • 133
  • 3
  • 12
  • did you check the value of `myAdap.getPosition(myString)` in line 4? – saschoar Jan 15 '13 at 10:43
  • yes, its always 1. Am not sure why that is. – Jasma Jan 15 '13 at 10:58
  • instead of passing String value of spinner in extra y dont you pass the selected index in extras so that you can directly set it. – Siddhesh Jan 15 '13 at 11:02
  • try using new ArrayAdapter so it rebinds the string everytime it is updated. – SingleWave Games Jan 15 '13 at 11:04
  • check out this answer http://stackoverflow.com/a/2390140/1438915 its similar what you wnat. – Siddhesh Jan 15 '13 at 11:05
  • am getting it from the DB (a SQLite). this line - String myString = getIntent().getStringExtra("Currency"); , fetches the values from the DB - either as INR, USD or EUR. I am not storing the index during the ADD earlier anyways. – Jasma Jan 15 '13 at 11:06
  • @Siddhesh - yes, i did check that post and my code is a copy paste of that. It still didnt work and i was looking for help as to why that is so. – Jasma Jan 15 '13 at 11:13
  • make an arraylist of array which you ar e suppling to adapter and find arraylist.indexOf(myString); and then set the returened index for selection of spinner – Siddhesh Jan 15 '13 at 11:26
  • I have updated my code in the question. Now the spinnerPosition is picking up the right number - but the spinner still shows the default value. What am i missing? – Jasma Jan 17 '13 at 07:08

1 Answers1

2

I was able to solve it using this updated Code. This is for anyone else who is looking for this solution

                // 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);

            spinnerPosition = 0;
            String myString = getIntent().getStringExtra("Currency");
            spinnerPosition = adapter.getPosition(myString);
            currencySpin.setSelection(spinnerPosition);
Jasma
  • 133
  • 3
  • 12