0

In strings.xml I have string-array which populates the spinner. I have an note app where user can select subject from spinner and show in recycler view. So after user added note and if the user clicks the note then it should redirect to that spinner and auto select the subject which is selected previously.

I tried to set position as sub_spinner.setSelection(subPosition) but I can't figure out how to get the subPosition

String.xml

<string-array name="subject">
    <item>Communication English</item>
    <item>Computer Graphics</item>
    <item>COA</item>
    <item>Data Communication</item>
    <item>Instrumentation II</item>
    <item>Software Engineerinng</item>
</string-array>

So if I have sub as COA then i should get index of 2.

Simson
  • 3,373
  • 2
  • 24
  • 38
Pawan Acharya
  • 89
  • 1
  • 14
  • 3
    try this `List list= Arrays.asList(getResources().getStringArray(R.array.subject));` you must add this list to ArrayAdapter, and set this adapter to spinner. Overrride the OnItemSelected method on this spinner there youll get position of item clicked. Follow [this](https://stackoverflow.com/questions/16581536/setonitemselectedlistener-of-spinner-does-not-call) – Mohammed Farhan Oct 14 '19 at 03:59

1 Answers1

1

Since you have the subject string-array in the xml. You can get the string array in your java code by using

List<String> list= Arrays.asList(getResources().getStringArray(R.array.subject));

Then in order to get the index of the selected subject, you can can implement an OnItemSelectedListener on the Spinner or ListView; anything that you have populated your string array on.

The OnItemSelectedListener implementation has a callback like onItemSelected with parameters, you want the position which provides the position of the selected item. Then you can do:

setSelection(position)

inside of the OnItemSelectedListener. If you need further guidance on how to implement, you can post some more code and I will provide you the exact code to do this.

ravi
  • 899
  • 8
  • 31