Spinner with states and cities?

3

How does a spinner work with states and cities in Brazil? When you click on the state, list the cities?

How to do this? A bank with everything registered? SQLite? Do I need a ready-made model for states and cities?

    
asked by anonymous 01.12.2014 / 03:01

1 answer

3

Just create a table (SQLite) with the states and another with the cities, interconnected by a foreign key and their objects clear.

Of course you should already have the states and cities already registered in the bank.

Then just get the event of spinner states, the example is:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View view, int pos, long id) 
    {
        Estado es = (Estado) parentView.getItemAtPosition(pos).

        // Código de pesquisa na Tabela cidades das que tiverem o estado selecionado. 
        // Exemplo:
        ArrayList<Cidade> listaCidades = buscarNoBanco(es);

        // Depois é só adicionar a listaCidades a um outro spinner 
        // ou uma listView de cidades.
    }
});
    
01.12.2014 / 12:00