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

9
  • did you check the value of myAdap.getPosition(myString) in line 4? Commented Jan 15, 2013 at 10:43
  • yes, its always 1. Am not sure why that is. Commented Jan 15, 2013 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. Commented Jan 15, 2013 at 11:02
  • try using new ArrayAdapter so it rebinds the string everytime it is updated. Commented Jan 15, 2013 at 11:04
  • check out this answer stackoverflow.com/a/2390140/1438915 its similar what you wnat. Commented Jan 15, 2013 at 11:05

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.