Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set position in spinner?

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.

like image 902
Adam Avatar asked Jan 07 '12 11:01

Adam


People also ask

How do I set the spinner value in Kotlin?

This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is spinner in Android with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.

What is the use of spinner in Android Studio?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


3 Answers

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; } 
like image 193
Adam Avatar answered Sep 20 '22 18:09

Adam


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); 
like image 34
Harsh Trivedi Avatar answered Sep 21 '22 18:09

Harsh Trivedi


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"));
like image 32
MohamedZaatari Avatar answered Sep 19 '22 18:09

MohamedZaatari