Issue
I need to make a RetrofitCall in an adapter so I can run a loop inside of a onBindViewHolder like this:
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());
}
});
So I can call this:
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 issue is that the retrofit call is not done before that code runs, so it gives null pointer exception and the app crashes. Any guess of what I can do? I've tried with several answers with no luck(Asynctask and a dependency called Rxjava).
Please keep in mind I'm a beginner, so try to be as much precise as you can.
Solution
I got an answer in spanish StackOverflow:
Retrofit executes the onResponse method asynchronous so you need and event that executes when the onResponse method obtains the answer.
Create an interface, call it OnOfertasResponse
:
public interface OnOfertasResponse
{
void ofertas(ArrayList<Oferta> ofertas);
}
The 'ofertas' method will execute when you get the server's response. This is known as callback
Now modify the 'checkguardadas' method so it accepts the callback, and in the onResponse execute the 'ofertas' method
public void checkguardadas(int id_usuario, OnOfertasResponse callback) {
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());
// ejecutamos el callback
callback.ofertas(response.body());
}
@Override
public void onFailure(Call<ArrayList<Oferta>> call, Throwable t) {
Log.d("traza", "por alla");
Log.d("traza", t.toString());
}
});
}
So to use it you just need to send the callback to 'checkguardadas' method
public void onBindViewHolder(final OfertasAdapter.MyViewHolder viewHolder, int i) {
// le enviamos el callback al metodo checkguardadas
checkguardadas(11,new OnOfertasResponse(){
@Override
public void ofertas(ArrayList<Oferta> ofertas){
// este metodo se ejecutara cuando onResponse se ejecute
Iterator it = ofertas.iterator();
while (it.hasNext()) {
if (ofertaList.get(i).getId() == ofertas.get(i).getId()) {
viewHolder.guardar.setChecked(true);
}
}
}
})
Answered By - Diego Romero
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.