Can you use value in a string-array to assign a different identifier to each item of an Android spinner?

3

I happen to have a Spinner that has 4 names:

   <string-array name="Nombres">
        <item>Seleccione</item>
        <item>Juan</item>
        <item>Pedro</item>
        <item>Carlos</item>
        <item>Jose</item>
    </string-array>

I want to save them in a MySQL database, but the field to save is of type int , since it is my foreign key to relate that table with another one. In HTML I usually do it like this:

   <select class="nombre" name="nombre">
      <option>Seleccione</option>
      <option value="1">Juan</option>
      <option value="2">Pedro</option>
      <option value="3">Carlos</option>
      <option value="3">Jose</option>
   </select>

My question is: can this be done on Android ?:

   <string-array name="Nombres">
        <item>Seleccione</item>
        <item value="1">Juan</item>
        <item value="2">Pedro</item>
        <item value="3">Carlos</item>
        <item value="4">Jose</item>
    </string-array>

I have not found a similar example, that's why I ask it.

Right now, I'm doing it like this:

Spinner spinner_nombres = (Spinner)findViewById(R.id.sp_nombre);

ArrayAdapter adapter_d = ArrayAdapter.createFromResource(this, R.array.Nombres, R.layout.spiner_item);
spinner_nombres.setAdapter(adapter_d);

String selec = spinner_nombres.getSelectedItem().toString();
        int id_nombre = 0;
        if (selec.equals("Juan")) {
            id_nombre = 1;
        } else
        if (selec.equals("Pedro")) {
            id_nombre = 2;
        }
        else
        if (selec.equals("Carlos")) {
            id_nombre = 3;
        }
        else
        if (selec.equals("Jose")) {
            id_nombre = 4;
        }

And so I send it to PHP:

nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("idnombre",Integer.toString(id_nombre).trim()));

The detail is that I am using 14 Spinners in my application and both code starts to drive me crazy, so I would really appreciate it if someone can help me.

    
asked by El Cóndor 10.06.2016 в 07:01
source

2 answers

2

If you are going to load the data in the Adapter in this way, it is not possible.

ArrayAdapter adapter_d = ArrayAdapter.createFromResource(this, R.array.Nombres, R.layout.spiner_item);

The option would be to create two string-array within styles.xml , one for the names:

  <string-array name="nombres">
        <item>Seleccione</item>
        <item>Juan</item>
        <item>Pedro</item>
        <item>Carlos</item>
        <item>Jose</item>
    </string-array>

and another one for the values:

  <string-array name="val_nombres">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>

In the Adapter loads the names.

ArrayAdapter adapter_d = ArrayAdapter.createFromResource(this, R.array.nombres, R.layout.spiner_item);

to get the value of a name you do it by means of the position:

Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
int spinner_posicion = spinner.getSelectedItemPosition();
String[] valores_nombre = getResources().getStringArray(R.array.val_nombres);
int valor_nombre = Integer.valueOf(valores_nombre[spinner_posicion]);

You could use the values of string-array containing the attributes that contain the values, but you would have to implement your own parser .

  <string-array name="Nombres">
        <item>Seleccione</item>
        <item value="1">Juan</item>
        <item value="2">Pedro</item>
        <item value="3">Carlos</item>
        <item value="4">Jose</item>
    </string-array>

Actually, it is much simpler and better to perform the option of two string-array .

    
answered by 10.06.2016 / 19:21
source
1

There is no easy way to do it in an XML file. As an alternative, you can use a List<Pair<Integer, String>> filling it with code.

The Pair documentation is here: link

    
answered by 10.06.2016 в 11:53