Android String-arrays create Item with Id -


How to create an Android XML file like this selection list:

  & lt; Select ID = ".. T" name = "..." & gt; & Lt; Option value = "1" & gt; Excellent & lt; / Options & gt; & Lt; Option value = "2" & gt; Good & lt; / Options & gt; & Lt; Option value = "3" & gt; Avg & lt; / Options & gt; & Lt; Option value = "4" & gt; Bad & lt; / Options & gt; & Lt; Option value = "5" & gt; Very bad & lt; / Option & gt; & Lt; / Select & gt;  

Is this code valid?

  & lt; String-array name = "..." & gt; & Lt; Item value = "10" & gt; Excellent & lt; / Item & gt; & Lt; Item value = "8" & gt; Good & lt; / Item & gt; & Lt; Item value = "5" & gt; Avg & lt; / Item & gt; .... & lt; / String-arrays & gt;  

If the value of the selected item is found in the Java code, is it valid? Since the array index always ranges from 0 to n-1 (n = counting the item), and

You can only define one-dimensional vectors in resources, you can not use the index as values.
At least, if these values ​​are meant to represent "custom" values.

But you can use parallel array and can add value to details using a normal index.

It is very beautiful that how ListPreferences work:

  android: entries = "@ array / prefs_send_titles" android: entryValues ​​= "@ Array / prefs_send_values ​​" 

You have 2 different arrays in your resources: one for details and one for values.


To get the value from the selected item, declare an integer to be the value:

  int value = 0;  

Use the above switch ... case :

  Automatically selected items on public zeros (adapterview & lt; ? & Gt; Parent, View View, Integer Status, Long ID) {int pos = mySpinner.getSelectedItemPosition (); Switch (pause) {case 0: value = 5; break; Case 1: Value = 8; break; Case 2: Value = 11; break; }}  

Or you can use values ​​from an array. I am not showing this technique because it will be more time to write, but it is comfortable.


To set the spinner based on desired value based on the desired description (this is its initial regular):

  try {int pos = 0; Switch (value) {case 5: paus = 0; break; Case 8: Position = 1; break; Case 11: Status = 2; break; } MySpinner.setSelection (pos); } Hold (last exception e) {mySpinner.setSelection (0); }  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -