Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Created March 24, 2017 22:02
Show Gist options
  • Save jacksonfdam/5582ffd5291eec124161ff031a7bc45f to your computer and use it in GitHub Desktop.
Save jacksonfdam/5582ffd5291eec124161ff031a7bc45f to your computer and use it in GitHub Desktop.
MainActivity + Atualizador - Thread
package br.com.targettrust.aula0801;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView lista;
List<Pessoa> listaUsuarios = new ArrayList<Pessoa>();
PessoaAdapter adapter;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView)findViewById(R.id.lista);
adapter = new PessoaAdapter(listaUsuarios,getBaseContext());
lista.setAdapter(adapter);
Log.v("Main","Possui " + listaUsuarios.size());
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Main","Click " + listaUsuarios.size());
adapter = new PessoaAdapter(listaUsuarios,getBaseContext());
lista.setAdapter(adapter);
}
});
}
@Override
public void onResume(){
super.onResume();
Atualizador atualizador = new Atualizador();
new Thread(atualizador).start();
listaUsuarios = atualizador.getListaPessoas();
Log.v("Main","Possui " + listaUsuarios.size());
adapter = new PessoaAdapter(listaUsuarios,getBaseContext());
lista.setAdapter(adapter);
}
public class Atualizador extends Thread{
List<Pessoa> lista = new ArrayList<Pessoa>();
public Atualizador(){
}
public List<Pessoa> getListaPessoas(){
return lista;
}
public void run(){
String linha;
String resultado = "";
try {
URL url = new URL("https://randomuser.me/api/?results=5");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((linha = in.readLine()) != null) {
resultado += linha;
}
in.close();
Log.v("Thread", resultado);
JSONObject ResultadosAPI = new JSONObject(resultado);
JSONArray chaveResultados = ResultadosAPI.getJSONArray("results");
Log.v("JSON", "Possui " + chaveResultados.length() + " resultados.");
for (int pos = 0; pos < chaveResultados.length(); pos++ ) {
JSONObject usuario = chaveResultados.getJSONObject(pos);
Pessoa pessoa = new Pessoa();
pessoa.nome = usuario.getJSONObject("name").get("first").toString() + " " + usuario.getJSONObject("name").get("last").toString();
pessoa.foto = usuario.getJSONObject("picture").get("large").toString();
pessoa.email = usuario.getString("email");
pessoa.genero = usuario.getString("gender");
lista.add(pessoa);
}
} catch (Exception e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run(){
adapter.notifyDataSetChanged();
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment