Spinner and EditText

1

I have this code to load a spinner with data from a database:

private class Getfrutas extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Spinner.this);
            pDialog.setMessage("Obtencion de las seccion..");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandler jsonParser = new ServiceHandler();
            String json = jsonParser.makeServiceCall(URL_LISTA_FRUTA, ServiceHandler.GET);
            Log.e("Response: ", "> " + json);
            if (json != null) {
                try {
                    JSONObject jsonObj = new JSONObject(json);
                    if (jsonObj != null) {
                        JSONArray seccion = jsonObj
                                .getJSONArray("frutas");

                        for (int i = 0; i < seccion.length(); i++) {
                            JSONObject catObj = (JSONObject) seccion.get(i);
                            Seccion cat = new Seccion(catObj.getInt("id_seccion"),
                                    catObj.getString("seccion"));

                            frutasList.add(cat);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("JSON Data", "¿No ha recibido ningún dato desde el servidor!");
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();
            populateSpinner();
        }
    }


    public void onItemSelected(AdapterView<?> parent, View view, int position,
                               long id) {
        Toast.makeText(
                getApplicationContext(),
                parent.getItemAtPosition(position).toString() + " Seleccionada" ,
                Toast.LENGTH_LONG).show();
        String seccion = parent.getItemAtPosition(position).toString();
        k.setText(seccion);


    }
    public void onNothingSelected(AdapterView<?> arg0) {
    }

As you can see I am capturing the data called section, something like this:

What I need is that in the text field the ID of the data selected in the spinner is shown, any idea of how I can do it?

    
asked by jaron cascante Pérez 02.11.2016 в 18:28
source

1 answer

3

From what I see in your code, you use the frutasList list to fill the spinner. Quickly to answer your question of putting the id of the selected item in the edittext, it would be done this way

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    k.setText(frutasList.get(position).id); // <-- asumiendo que la clase Seccion tiene un atributo "id" y es tipo String
}

According to the selected position of the spinner, you select the object in the list frutasList and you get the attribute id of the object Section

    
answered by 02.11.2016 / 18:58
source