简体   繁体   中英

How to get string value from string-array in strings.xml

I have a spinner with some string arrays and they have some values = emails, and I need to get email (value)when specific item(string-array) selected in spinner, but all I got its just a string (Name/Title in the string array), all I need its just how to get value of a string in string-array. the code of string-array looks like that

<string-array name="choose_array"> 
 <item>Choose</item>
 <item value = "something@mail.com">somethingName</item>
 <item value = "something@mail.com">somethingName</item>
 </string-array>

I need to get the value (email) of the selected string to work with it but I dont know how. I tried some ways to get it but all I get its just string (somethingName in code)

   <string-array name="progress_array">
        <item>Not Done</item>
        <item>Done</item>
        <item>On Hold</item>
        <item>In Progress</item>

    </string-array>

and the code for spinner is

 spinner = (Spinner) findViewById(R.id.selection);
            // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.progress_array, android.R.layout.simple_spinner_item);
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            spinner.setAdapter(adapter);
}

  public boolean doTask(){
     String spinnerValue = (String)spinner.getSelectedItem();
            if(spinnerValue.equals("Done")){
                spinnerInd = 1;

            }else if(spinnerValue.equals("Not Done")){
                spinnerInd = 0;
            }else if(spinnerValue.equals("On Hold")){
                spinnerInd = 2;
            }else if(spinnerValue.equals("In Progress")){
                spinnerInd = 3;
            }

}

Strings.xml

<string-array name="your_array_string">  
<item>Item1</item>
<item>Item2</item>
</string-array>

Convert to string array setOnItemSelectedListener you can get the value

 String[] your_array = getResources().getStringArray(R.array.your_array_string);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
               String selectedValue=your_array[position];
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // Your code here
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM