There are 2 ways in which you can achieve it:
ArrayAdapter<String>
is used to add options to spinners with an array of string where each element of the array is an element of the spinnner. You would load the provinces based on the selected index:
Spinner estadosSpinner = (Spinner)findViewById(R.id.estados_spinner);
final Spinner provinciasSpinner = (Spinner)findViewById(R.id.provincias_spinner);
ArrayList<String> estados = new ArrayList<String>();
estados = obtenerEstados();
ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_2, estados);
// le asignamos el adapter al spinner
estadosSpinner.setAdapter(adapter);
estadosSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//cargas las provincias por el indice del estado seleccionado
ArrayList<String> provincias = cargarProvinciasPorPosicionEstado(position);
// Aqui inicializas el adaptador con las provincias
ArrayAdapter<String> provinciasAdapter = new ArrayString>(ActivityName.this,android.R.layout.simple_expandable_list_item_2, provincias);
provinciasSpinner.setAdapter(provinciasAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here is a tutorial more detailed and in Proyecto Simio have another very good
Custom adapter
This is that for each element of an array with its own data type, you load a view and assign the data to the view with each element of the array:
This class represents both states and provinces:
public class Localizacion
{
private String nombre;
private int id;
public String getNombre()
{
return this.nombre;
}
public void setNombre(String name)
{
this.nombre = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
We create a localizacion.xml
view that represents a visual location for both states and provinces as well:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:text="@string/nombre"
android:gravity="center|left"
android:textStyle="bold"
android:layout_height="match_parent" />
</LinearLayout>
Now we create our own ArrayAdapter that would load each element of the spinner:
public class SimpleSpinnerAdapter extends ArrayAdapter
{
// localizacion reprenseta un estado o prinvincia
private ArrayList<Localizacion> localizaciones;
public PersonAdapter(@NonNull Context context, ArrayList<Localizacion> localizaciones) {
super(context, android.R.layout.simple_expandable_list_item_2 );
this.localizaciones = localizaciones;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// preguntamos si la vista es null para cargar la vista desde el xml
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.localizacion, parent, false);
}
TextView nombre= convertView.findViewById(R.id.nombre);
// Guardamos la data de la localizacion para al momento de seleccionar, podamos extraer cual fue
convertView.setTag(this.localizaciones.get(position));
nombre.setText(this.localizaciones.get(position).getNombre);
return convertView;
}
}
Now in the activity, we have to assign the data of the locations to the adapter and add the event setOnItemClickListener
and load the provinces according to the state that is selected. The same adapter is assigned to both spinners since they are the same:
Spinner estadosSpinner = (Spinner)findViewById(R.id.estados_spinner);
final Spinner provinciasSpinner = (Spinner)findViewById(R.id.provincias_spinner);
ArrayList<Localizacion> estados = new ArrayList<Localizacion>();
estados = obtenerEstados();
SimpleSpinnerAdapter adapter = new SimpleSpinnerAdapter(this, estados);
estadosSpinner.setAdapter(adapter);
estadosSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// obtenemos la localizacion que guardamos en el adapter en la propiedad tag
Localizacion estado = (Localizacion)view.getTag();
//cargas las provincias por el indice del estado seleccionado
ArrayList<Localizacion> provincias = cargarProvinciasPorEstado(estado.getId());
// Aqui inicializas el adaptador con las provincias
SimpleSpinnerAdapter provinciasAdapter = new SimpleSpinnerAdapter(ActivityActual.this, provincias);
provinciasSpinner.setAdapter(provinciasAdapter);
}
});
In simian project you have a tutorial that can help you.