I have to do a Retrofit Call inside an adapter to compare information from two arraylist:
public void checkguardadas(int id_usuario) {
RetrofitService retrofitService = RetrofitService.getInstance();
PabloAPI api = retrofitService.getApiProxyServer();
Call<ArrayList<Oferta>> call = api.getGuardadas(1);
call.enqueue(new Callback<ArrayList<Oferta>>() {
@Override
public void onResponse(Call<ArrayList<Oferta>> call, Response<ArrayList<Oferta>> response) {
Log.d("traza", "por aqui");
Log.d("traza", response.body().toString());
guardadas = response.body();
}
@Override
public void onFailure(Call<ArrayList<Oferta>> call, Throwable t) {
Log.d("traza", "por alla");
Log.d("traza", t.toString());
}
});
And so, I call this later.
public void onBindViewHolder(final OfertasAdapter.MyViewHolder viewHolder,
int i) {
Iterator it = guardadas.iterator();
while (it.hasNext()) {
if (ofertaList.get(i).getId() == guardadas.get(i).getId()) {
viewHolder.guardar.setChecked(true);
}
}
The problem is that the call is not executed before the code is run, so it gives null value and the application does not open. Any idea what I can do? I have tried several things without much luck (AsyncTask and a dependency Rxjava) and also put all the adapter in onResponse () but I would not know how to do it. Please take into account that I am a beginner!