Created
July 27, 2018 04:47
-
-
Save Guilherme-HRamos/0f26b3c34031b381a83b4594dd8de7dd to your computer and use it in GitHub Desktop.
Carregamento de estados e suas respectivas cidades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mport android.content.Context; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* Classe criada por Guilherme Ramos em 2018 | |
*/ | |
public class JsonReaderUtil { | |
public static String loadJSONFromAsset(Context context) { | |
String json = null; | |
try { | |
// necessário disponibilizar o JSON em assets/jsons/estados_cidades.json | |
InputStream is = context.getAssets().open("jsons/estados_cidades.json"); | |
int size = is.available(); | |
byte[] buffer = new byte[size]; | |
is.read(buffer); | |
is.close(); | |
json = new String(buffer, "UTF-8"); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
return null; | |
} | |
return json; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Classe criada por Guilherme Ramos em 2018 | |
*/ | |
public class StatesAndCitiesLoader extends AsyncTask<Context, Void, String> { | |
private final StatesAndCitiesLoader.LoadCitiesCallback callback; | |
public StatesAndCitiesLoader(StatesAndCitiesLoader.LoadCitiesCallback callback) { | |
this.callback = callback; | |
} | |
@Override | |
protected String doInBackground(Context... voids) { | |
return JsonReaderUtil.loadJSONFromAsset(voids[0]); | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
callback.onFinished(new Gson().fromJson(s, StatesAndCitiesModel.class)); | |
} | |
public interface LoadCitiesCallback { | |
void onFinished(StatesAndCitiesModel model); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Classe modelo responsável pelo modelo feito no JSON | |
*/ | |
public class StatesAndCitiesModel { | |
private List<EstadosModel> estados; | |
public List<EstadosModel> getEstados() { | |
return estados; | |
} | |
public void setEstados(List<EstadosModel> estados) { | |
this.estados = estados; | |
} | |
public static class EstadosModel { | |
private String sigla; | |
private String nome; | |
private List<String> cidades; | |
public String getSigla() { | |
return sigla; | |
} | |
public void setSigla(String sigla) { | |
this.sigla = sigla; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public List<String> getCidades() { | |
return cidades; | |
} | |
public void setCidades(List<String> cidades) { | |
this.cidades = cidades; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment