Get the id of my ArrayList and put it in a TexView

1

I would like to get the id of the selected item in a Spinner

 ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_list_item_1,spacecrafts);
        sp.setAdapter(adapter);


        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(c,spacecrafts.get(position),Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

This is where the value is assigned to spacecraft

private int parseData()
{
    try {
        JSONArray ja=new JSONArray(jsonData);
        JSONObject jo=null;

        spacecrafts.clear();
        curso s=null;

        for(int i=0;i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);

            int id=jo.getInt("id");
            String name=jo.getString("name");

            s=new curso();
            s.setId(id);
            s.setName(name);

            spaceid.add(Integer.toString(id));
            spacecrafts.add(name);


        }

        return 1;

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return 0;

}
    
asked by Rodolfo Gav 04.12.2016 в 18:39
source

1 answer

1

Within onItemSelected() you can get it from the position (position), since spacecrafts is an array of objects curso , considering obviously that the object has a getter to get the Id:

spacecrafts.get(position).getId()

For example, using the Toast you would show it:

Toast.makeText(c,"El id es " + spacecrafts.get(position).getId(),Toast.LENGTH_SHORT).show();

To set the value to TextView

textView.setText(spacecrafts.get(position).getId());
    
answered by 04.12.2016 в 18:42