Set spinner value from array to strings.xml

I have a spinner that uses an array from strings.xml

if the array has 5 rows (1,2,3,4,5) and I want the counter to show the second string (2) as the default, is this possible? I know that I can arrange the order of the lines so that the first is 2, but this does not look very good if the spinner dialog is displayed as (2,1,3,4,5).

Or should an array be created in my activity programmatically and then use setPostion ()? I tried this, but getting an error while creating an array in action. Can someone please give me an example of how to create an array and use it in spinner ()

I also searched for answers here, but didn't seem to find what I needed.

Thanks for watching ....

+4
source share
2 answers

I recommend you check: http://developer.android.com/resources/tutorials/views/hello-spinner.html

You must create an ArrayAdapter in your activity.

From the above link:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } 

you can use

 spinner.setSelection(adapter.getPosition(value))); 

to set the position.

+12
source

Cheers for the answer .....

I did not do what I needed, but I managed to solve my problem as follows.

First I created an array inside my activity, not strings.xml.

  String[] NoCore_Array = new String [5]; { NoCore_Array[0] = "1"; NoCore_Array[1] = "2"; NoCore_Array[2] = "3"; NoCore_Array[3] = "4"; NoCore_Array[4] = "5"; } 

Then I created a counter using ...

  Spinner spnNoCore = (Spinner) findViewById(R.id.spinner_nocore); 

Then the adapter created using the above array ....

  ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, NoCore_Array); spnNoCore.setAdapter(NoCoreAdapter); 

Then set the default adapter position as follows:

  //Set Default Selection spnNoCore.setSelection(1); 

Then the rest of the spinner code for action ...

  spnNoCore.setOnItemSelectedListener(new OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { //Get item from spinner and store in string conductorSize........ NoCore = parent.getItemAtPosition(pos).toString(); if (NoCore.equals(NoCore1)) { CoreNo = 1 ; } if (NoCore.equals(NoCore2)) { CoreNo = 2 ; } if (NoCore.equals(NoCore3)) { CoreNo = 3 ; } if (NoCore.equals(NoCore4)) { CoreNo = 4 ; } if (NoCore.equals(NoCore5)) { CoreNo = 5 ; } } public void onNothingSelected(AdapterView parent) { // Do nothing. } }); 

Hope this can help other people who have the same problem with setting the default value to Spinner.

However, the layout of the dialog is not so good when it is done. There are no radio buttons, it just looks like a selection of text with lines separating the sections.

+3
source

Source: https://habr.com/ru/post/1386377/


All Articles