28

I read with BufferedReader text from a system file; this text contains, for example, 5 WORDS, but in another case it can contain fewer or more words. Then I put this text (the mentioned words) into a SINGLE string and saved that string to shared preferences. Then I made a spinner from this string. Code is as follows:

Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourString.split(" "));
    spinner.setAdapter(spinnerArrayAdapter);

Then I read text from another file. This text always contains one word. And this word is the same as one of the words which I had read from the first file (for example, if the first file which I read contained 5 words and one of these words was "black", the second file which I read also contains "black"). And I need to make this particular word (which exists in both files) as the default chosen option in my spinner.

For example:

First string contains: red, blue, yellow, black, white

Second string contains: yellow

I make a spinner from first string so options in spinner are populated exactly like this: "red, blue, yellow, black, white" and the default selected option is red (because it happens to be the first one in my first string), but I need to make yellow as the default selected option in this case, because second string contains "yellow". The words in both string are always different.

BTW: I know how to save the position in a spinner, but I don't know how to set the position in a spinner if I compare two strings and one of them contains more words.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
Adam
  • 2,152
  • 4
  • 35
  • 45

3 Answers3

63

Here is solution, thanks to sfratini for help.

Use:

spinner.setSelection(getIndex(spinner, myString));

Then:

private int getIndex(Spinner spinner, String myString){

        int index = 0;

        for (int i=0;i<spinner.getCount();i++){
            if (spinner.getItemAtPosition(i).equals(myString)){
                index = i;
            }
        }
        return index;
}
Adam
  • 2,152
  • 4
  • 35
  • 45
  • 2
    You could optimize this, by returning index directly when found. Saves you a variable plus some iterations. But only if this suits your use case. (Returns the first match, not the last, as it is right now) – AlbAtNf Jan 11 '16 at 16:01
15

i think this line will help you

 String[] a= new String[10];
 a[0]="abc";
 a[1]="xyz";
 a[2]="pqr";
 .....
 .....
 spin = (Spinner) findViewById(R.id.TimeSpinner);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(TimeSpin.this, android.R.layout.simple_spinner_item, a);  
   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)‌​;  
   spin.setAdapter(adapter); 
   spin.setSelection(0);
Harsh Trivedi
  • 1,012
  • 8
  • 25
  • ask me if you have any further doubt – Harsh Trivedi Jan 07 '12 at 12:00
  • No, I only need to setPosition, but this is other case, first I read text from one file (for example this text contains 5 words and the third of these words is "black") I save these 5 words to one string. Then I make a spinner from this string as you advised me. Then I read text from another file and this text contains always one word which is the same as one word in first file(for example second file contains "black") I save this one word to String. And now I need to make "black" as default position in my spinner. Do you understand me? – Adam Jan 07 '12 at 12:18
  • first of all if you have to set any data as default in spinner you have to create an Array or list because spinner.setselection method get input in only int. so At that time you have to pass that Array or list index to this method. got it ? – Harsh Trivedi Jan 07 '12 at 12:22
  • you can not set default value as String to spinner – Harsh Trivedi Jan 07 '12 at 12:24
  • First string contains 5 words and from this string I make spinner. Second string contains one word (this word is the same as ONE word in first string) and I need to make this word as default option in spinner. – Adam Jan 07 '12 at 12:31
  • yes i understand but you have to store this String values because without index you cant set direct String value to any spinner – Harsh Trivedi Jan 07 '12 at 12:57
  • String[] a= new String[2]; a[0]="abc"; a[1]="xyz"; a[2]="pqr"; spin = (Spinner) findViewById(R.id.TimeSpinner); ArrayAdapter adapter = new ArrayAdapter(TimeSpin.this, android.R.layout.simple_spinner_item, a); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(adapter); spin.setSelection(0); – Harsh Trivedi Jan 07 '12 at 13:21
  • This is not what I need, look: first string: "black, yellow, blue, red, brown" second string: "blue" from first string I made spinner and if second string contains "blue" I need to make blue as default selected option in spinner. – Adam Jan 07 '12 at 13:33
  • so when you next time come you want to see blue as default value of spinner ?? – Harsh Trivedi Jan 07 '12 at 13:35
  • Yes if second string contains "blue" I always want to have blue as default selected option in my spinner when I open my activity. But if second string contains red, I want to have red as default selected option in spinner when I open my activity. – Adam Jan 07 '12 at 13:40
  • ok then you have to save this selected String in shared preferences and at the time of load Activity you have to fetch String from Sharedpreferences and add to an array after that follow my above ans your query will be solved .. – Harsh Trivedi Jan 07 '12 at 13:47
  • Doesn't work, I got a force close. I edited my question, I have posted an example there. – Adam Jan 07 '12 at 14:19
7

You don't have to use Adapter... you just need to convert String array to List
And then use indexOf(Object object) to get the index of you spin using selected color

String [] strings = yourString.split(" ");
List<String> strList = new ArrayList<String>(Arrays.asList(strings));
//Or you can load array from xml...
//List<String> strList2 = Arrays.asList(getResources().getStringArray(R.array.array_color));
spinner.setSelection(strList.indexOf("colorvalue"));
MohamedZaatari
  • 434
  • 4
  • 5