1

I posted similar question recently but nobody helped me. So I'll try to explain my problem better than before.

So I read some text from one file (this file may include more words) and then I read text from second file (this file always includes one word which is the same as one word in first file). Text in both files may be different everytime.

So for example:

First string contains: black blue yellow red green

Second string contains: yellow

Then I make spinner from first string, so spinner contains in this example these words (black blue yellow red green), so default option is black (coz it is first in my array), but I need to make third position as default in my spinner in this example, because second string contains yellow and yellow is on the third position in my spinner.

How can I make it without repopulating spinner?

btw. these strings are only example. Files may always includes different words than before.

Solution:

s1.setSelection(getIndex(s1, prefNameCurGOV));

and then:

private int getIndex(Spinner s1, String prefNameCurGOV){

        int index = 0;

        for (int i=0;i<s1.getCount();i++){
            if (s1.getItemAtPosition(i).equals(prefNameCurGOV)){
                index = i;
            }
        }
        return index;
Adam
  • 2,152
  • 4
  • 35
  • 45

1 Answers1

2

Something like:

String secondString = secondSpinner.getSelectedItem();

firstSpinner.setSelection(getIndex(firstSpinner,secondString));

then use

private int getIndex(Spinner spinner,String string){

//Pseudo code because I dont remember the API

int index = 0;

for (int i = 0; i < firstSpinner.size(); i++){

if (firstSpinner.getItemAtPosition(i).equals(string)){
   index = i;
}

}

return index;

}
sebastianf182
  • 9,844
  • 3
  • 34
  • 66