1

this is my code in strings.xml:

<string-array name="day_array">
    <item name="sh">Sunday</item>
    <item name="ye">Monday</item>
    <item name="do">Tuesday</item>
    <item name="se">Wednesday</item>
    <item name="ch">Thursday</item>
</string-array>
    <string-array name="time_array">
    <item name="00">00:00</item>
    <item name="01">01:00</item>
    <item name="02">02:00</item>
    <item name="03">03:00</item>
    <item name="04">04:00</item>
    <item name="05">05:00</item>
    <item name="06">06:00</item>
    <item name="07">07:00</item> 
    <item name="08">08:00</item>
    <item name="09">09:00</item>
    <item name="10">10:00</item>
    <item name="11">11:00</item>
    <item name="12">12:00</item>
    <item name="13">13:00</item>
    <item name="14">14:00</item>
    <item name="15">15:00</item>
    <item name="16">16:00</item>
    <item name="17">17:00</item>
    <item name="18">18:00</item>
    <item name="19">19:00</item>
    <item name="20">20:00</item>
    <item name="21">21:00</item>
    <item name="22">22:00</item>
    <item name="23">23:00</item>
    <item name="24">24:00</item>
</string-array>

and then in my layout i have two Spinner:

    <Spinner
        android:id="@+id/roozlist"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:entries="@array/day_array" />
    <Spinner
        android:id="@+id/timelist"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:entries="@array/time_array" />

what i want to do, is that, if the user chooses Sunday in "roozlist" and 08:00 in "timelist", and then he clicked on a button, i want the "sh08" to be returnd, in fact the name of each item he clicked, how can i assess to the name attribute on each item in my string-array?

2 Answers 2

4

You have to take two more string array for selected day and time respective value :

<string-array name="day_array">
   <item>Sunday</item>
</string-array>

<string-array name="day_array_value">
  <item>sh</item>
</string-array>

<string-array name="time_array_value">
  <item>00</item>
</string-array>

<string-array name="time_array">
  <item>00:00</item>
</string-array>

Initialize value array :

 String[] roozArrayValue = getResources().getStringArray(R.array.day_array_value);
 String[] timeArrayValue = getResources().getStringArray(R.array.time_array_value);

Get spinner selected item respective value from value array:

String value = roozArrayValue[roozlist.getSelectedItemPosition()]+timeArrayValue[timelist.getSelectedItemPosition()]

Check Example : Spinner String item value get (not string name) android

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

Comments

1

try these one

initialize some variable

String[] day;
String[] time;

and in your onCreate method

day = getResources().getStringArray(R.array.day_array);
time = getResources().getStringArray(R.array.time_array);

and then on your onClickButton

String out = day[roozlist.getSelectedItemPosition()]+""+time[timelist.getSelectedItemPosition()];

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.