Use Retrofit to popular a Recyclerview

Asked

Viewed 250 times

0

I need to bring up a list of cities in one RecyclerView of a WebService using the Retrofit. But my List this coming null.

I have a Class cidades with the data I need to take from WebService, to Class DadosCidades where do I set up Retrofit.

My Dataserver Interface:

public interface DataServer {
@GET("cidade-nome.php?nome=rio")
Call<List<Cidades>> getCidades();
}

The Citylistadapter Adapter:

public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.CityViewHolder> {

Context context;
List<Cidades> cidadesList;


public CityListAdapter(Context context, List<Cidades> cidadesList) {
    this.context = context;
    this.cidadesList = cidadesList;
}


@Override
public CityListAdapter.CityViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {


   View view = LayoutInflater.from(context).inflate(R.layout.single_row_view, viewGroup, false);

    return new CityViewHolder(view);
}

@Override
public void onBindViewHolder(CityListAdapter.CityViewHolder cityViewHolder, int i) {

    cityViewHolder.txtNome.setText(cidadesList.get(i).getNome());
    cityViewHolder.txtSigla.setText(cidadesList.get(i).getSigla_estado());
    cityViewHolder.txtLatitude.setText(cidadesList.get(i).getLatitude());
    cityViewHolder.txtLongitude.setText(cidadesList.get(i).getLongitude());

}

@Override
public int getItemCount() {

    return cidadesList.size();
}

public class CityViewHolder extends RecyclerView.ViewHolder {

    TextView txtNome;
    TextView txtSigla;
    TextView txtLatitude;
    TextView txtLongitude;

    public CityViewHolder(View itemView) {
        super(itemView);

        txtNome = itemView.findViewById(R.id.txtNome);
        txtSigla = itemView.findViewById(R.id.txtSigla);
        txtLatitude = itemView.findViewById(R.id.txtLatitude);
        txtLongitude = itemView.findViewById(R.id.txtLongitude);
    }
}
}

A xml single_row_view, with the TextView to the RecyclerView and my Activity where I want to display the list of cities.

Activity Searchactivity :

public class SearchActivity extends AppCompatActivity{

    RecyclerView recyclerView;
    CityListAdapter adapter;
    List<Cidades> cidadesList;

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

        cidadesList = new ArrayList<>();

        recyclerView = findViewById(R.id.recycler_view_notice_list);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        DataServer dataServer = DadosCidades.getDadosCidades().create(DataServer.class);

        Call<List<Cidades>> call = dataServer.getCidades();

        call.enqueue(new Callback<List<Cidades>>() {
            @Override
            public void onResponse(Call<List<Cidades>> call, Response<List<Cidades>> response) {

                cidadesList = response.body();

                Log.d("SearchActivity", cidadesList.toString());

                adapter = new CityListAdapter(getApplicationContext(), cidadesList);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onFailure(Call<List<Cidades>> call, Throwable t) {

            }
        });

    }

}

And finally xml activity_search:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.activity.SearchActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_notice_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

However cidadesList = new ArrayList<>(); comes size = 0 I’ve seen several tutorials and so far could not solve.

  • Already checked if the endpoint is coming the data?

  • Yes, the data is not enough, everything is null. And I do not understand the reason.

  • So, maybe the problem is in the API, try "hitting" this endpoint with Postman to retrieve the Iprs. If there you see the problem is in the app, if not probably in the API

1 answer

0

The onResponse method gives you only one return, however it may not have been the code 200 (as you would expect). Try playing log sponse.code() to see. Also, you can use an if with Sponse.isSuccessful() to be sure of the return and treat the exceptions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.