Display data in a TextView and change it every time you select another data from a Spinner

0

I want to show the data in the txtloc, but it always shows the first data of the spinner and does not change when I select another spinner data. On the other hand, the txtpro does change when I select another data inside its spinner ... How could I make both spinners save their data in a TextView and change each time I select another data within their spinner?

This is my code

 txtpro = (TextView) findViewById(R.id.txtpro);
    txtloc = (TextView) findViewById(R.id.txtloc);

    spinnerPro = (Spinner) findViewById(R.id.spinnerProvincia);
    spinnerLoc = (Spinner) findViewById(R.id.spinnerLocalizacion);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,
            R.array.array_provincias,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerPro.setAdapter(adapter);
    spinnerPro.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    int[] localidades = {R.array.array_sevilla, R.array.array_malaga};

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,
            localidades[i],
            android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLoc.setAdapter(adapter);

    txtloc.setText(spinnerLoc.getSelectedItem().toString());
    txtpro.setText(adapterView.getItemAtPosition(i).toString());

}

And these array I have in my String.xml file

  <string-array name="array_provincias">
        <item>Sevilla</item>
        <item>Malaga</item>
    </string-array>

    <string-array name="array_sevilla">
        <item>Tomares</item>
        <item>Camas</item>
    </string-array>

    <string-array name="array_malaga">
        <item>Casares</item>
        <item>Estepona</item>
    </string-array>
    
asked by Matías Nicolás Núñez Rivas 02.05.2018 в 16:10
source

1 answer

0

Use OnItemSelectedListener() in your Activity:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // Tu código aquí

   String selecteditem = spinner.getselecteditem().tostring();
   textview.settext(selecteditem);


    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // Tu código aquí
    }

});

Or of this menara:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
    long id) {

   String selecteditem = spinner.getSelectedItem().toString();
   textview.setText(selecteditem);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {        
}
    
answered by 02.05.2018 / 16:29
source