1

I have a spinner. I want to get the value of the item selected and search the index of that value in another array. This is what I have tried so far. s1 is the spinner. weapons is the array in which I want to search the value of the selected item in spinner s1

String[] weaponone = getResources().getStringArray(R.array.weapons);
for (String s : weaponone) {
    int i = s.indexOf(s1.getSelectedItem().toString());
    switch (i) {
        case (0):
8
  • I don't get the question ..? Commented Oct 18, 2015 at 4:21
  • You say you're looking for an exact string in your string array You don't define no constant for the value you're searching for in your array You're doing a switch case on a int instead of a string .. Commented Oct 18, 2015 at 4:21
  • in first line i am defining the array, in which i want to compare the string i got by selecting item on spinner s1 in third line. Commented Oct 18, 2015 at 4:28
  • Please explain your question and specify what u need, try to get to the point. Commented Oct 18, 2015 at 4:54
  • did you face any problem? Commented Oct 18, 2015 at 6:36

1 Answer 1

2

ok i got you situation ... in your case no need to use foreach loop focus on following example .. at first take a arrays.xml file under res/values folder and declare weapons array like this

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="weapons">
        <item>a</item>
        <item>b</item>
        <item>c</item>        
    </string-array>

</resources>

and your java code look like this

String[] weaponone = getResources().getStringArray(R.array.weapons);

    int i= Arrays.asList(weaponone).indexOf("b");   //i=1 for b, for a i=0 and for c i=2,if not found then i=-1 
        switch (i) {
            case (0):
               // implement your code 
                break;
            case (1):
               // implement your code 
                break;
            case (2):
               // implement your code 
                break;

           .................................


            case (-1):// when not matching 
               // implement your code 
                break;
        }

i hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.