Hello software developers.
I'm developing an android application that consumes a REST service so I'm using the Http Client Library (http://loopj.com/android-async-http/), but I want to populate a spinner control so I first call to the method fillSpinnerTiposSiniestros and populate an ArrayList to later use that ArrayList to fill the adapter of the spinner, but I do not know why the adapter is first established and then called the REST client, so the spinner remains empty.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_sinister);
editTextValorClave = (EditText) findViewById(R.id.editTextSinisterValorClave);
editTextValorCurp = (EditText) findViewById(R.id.editTextSinisterValorCurp);
editTextValorDependencia = (EditText) findViewById(R.id.editTextSinisterValorDependencia);
editTextValorRFC = (EditText) findViewById(R.id.editTextSinisterValorRFC);
editTextValorStatus = (EditText) findViewById(R.id.editTextSinisterValorStatus);
//editTextValorTipoSiniestro = (EditText) findViewById(R.id.editTextSinisterValorTipoSiniestro);
spinnerTipoSiniestro = (Spinner) findViewById(R.id.spinnerTipoSiniestro);
editTextValorNumAmbulancia = (EditText) findViewById(R.id.editTextSinisterNumAmbulancia);
editTextValorNombreSieniestro = (EditText) findViewById(R.id.editTextSinisterNombreSieniestro);
editTextValorLocalizacion = (EditText) findViewById(R.id.editTextSinisterValorLocalizacion);
textViewMuestraEncabezado = (TextView) findViewById(R.id.textViewTitulo);
botonAccionAddEdit = (Button) findViewById(R.id.buttonSinisterAddEdit);
botonAccionAddEdit.setOnClickListener(eventoAccionBotonAddEdit);
Bundle miBlunde = getIntent().getExtras();
opcion = miBlunde.getInt("Opcion");
claveGlobal = miBlunde.getInt("Clave");
CargarDatosSpinner;
}
private void rellenarSpinnerTiposSiniestros(){
List<Header> encabezados = new ArrayList<Header>();
encabezados.add(new BasicHeader("Accept", "application/json"));
SiniestroRESTClient.get(AddEditSinisterActivity.this, "api/TipoSiniestro", encabezados.toArray(new Header[encabezados.size()]), null,
new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response)
{
arrayTipoSiniestro = new ArrayList<TipoSiniestro>();
tipoSiniestroAdapter = new SpinnerTipoSiniestroAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, (TipoSiniestro[]) arrayTipoSiniestro.toArray());
for(int i = 0; i < response.length(); i++)
{
try
{
tipoSiniestroAdapter.add(new TipoSiniestro(response.getJSONObject(i)));
}
catch(JSONException e)
{
e.printStackTrace();
}
}
tipoSiniestroAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTipoSiniestro.setAdapter(tipoSiniestroAdapter);
}
});
}
I tried with this solution asynchttpclient gives response after activity, but I have an exception like:
FATAL EXCEPTION: AsyncTask #1 Process: com.maestria.lostperson, PID: 13433 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead. at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1493) at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:1095) at com.maestria.lostperson.clients.SiniestroRESTClient.get(SiniestroRESTClient.java:24) at com.maestria.lostperson.AddEditSinisterActivity$2.doInBackground(AddEditSinisterActivity.java:159) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)