Popular ListView with data from Retrofit2

1

I'm trying to pupolar a listView with information I get from WS through Retrofit2. But error somewhere and nothing is presented, neither error nor the listView. can give me strength for kindness.

Follow the code section.

    //inicio um Array list vazio
    list = new ArrayList<Course>();

    //configuro o Retrofit
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(CoursesInterface.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    CoursesInterface service = retrofit.create(CoursesInterface.class);


    Call<UdacityCatelog> requestCatalog =  service.listCatalog();


    //Metodo Assicrono
    requestCatalog.enqueue(new Callback<UdacityCatelog>() {
        @Override
        public void onResponse(Call<UdacityCatelog> call, Response<UdacityCatelog> response) {
            //verifico se esta tendo resposta
            if(!response.isSuccessful()){
                Log.i("Code", "Codigo: "+ response.code());
            }else{
                UdacityCatelog catalogo = response.body();
                for(Course c : catalogo.courses ){
                    //Verifico no log se as informações estão ok!
                    Log.i("Code", "course: "+ c.title);
                    //Adiciono a uma ListView
                    list.add(c);


                }
            }
        }

        @Override
        public void onFailure(Call<UdacityCatelog> call, Throwable t) {
            //Case de erro!
            Log.i("Code", "Codigo: "+ t.getMessage());
        }
    });


    //Vinculo a ListView
    listaItem = (ListView) findViewById(R.id.listItem);
    //configuro o adapter
    adapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            list
    );
    //Seto o Adapter
    listaItem.setAdapter(adapter);
    
asked by anonymous 14.01.2017 / 12:09

1 answer

2

Try this:

public void onResponse(Call<UdacityCatelog> call, Response<UdacityCatelog> response) {
            //verifico se esta tendo resposta
            if(!response.isSuccessful()){
                Log.i("Code", "Codigo: "+ response.code());
            }else{
                UdacityCatelog catalogo = response.body();
                for(Course c : catalogo.courses ){
                    //Verifico no log se as informações estão ok!
                    Log.i("Code", "course: "+ c.title);
                    //ADICIONE NO ADAPTER
                    adapter.add(c);


                }
              //NOTIFIQUE AS ALTERACOES
              adapter.notifyDataSetChanged();
            }
        }
    
14.01.2017 / 14:09