fill list within retrofit Onresponse

0

The project I'm doing is practically an application on android that I consume a ApiRestFull practically everything works fine I bring the elements set them and the response.body if the elements respond.

Error: When declaring a list of objects outside as global and inside the retrofit, trying to pass a list to another practically leaves it to null; I see it with debug and of course it brings elements.

public class MainActivity extends AppCompatActivity {

    List<Receta> receta = new ArrayList<Receta>();
    ListView listView;
    List<String> lsRecetaNombre = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        RecetaServicio servicio = retrofit.create(RecetaServicio.class);
        Call<List<Receta>> repos = servicio.listRepos("listar");

        repos.enqueue(new Callback<List<Receta>>() {
            @Override
            public void onResponse(Call<List<Receta>> call, Response<List<Receta>> response) {
                receta.addAll(response.body());
            }

            @Override
            public void onFailure(Call<List<Receta>> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Error al procesar la soliitud", Toast.LENGTH_LONG).show();
            }
        });

        lsRecetaNombre = listaDeNombres();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lsRecetaNombre);
        listView.setAdapter(adapter);

    }


    public List<String> listaDeNombres() {
        List<String> lsRecetaNombre = new ArrayList<String>();
        if (!receta.isEmpty()) {
            for (Receta r : receta) {
                lsRecetaNombre.add(r.getNombre());
            }
        }

        return lsRecetaNombre;
    }
}
    
asked by Monster 08.06.2018 в 02:59
source

0 answers