Fidelis
Fidelis

Reputation: 91

Android - set adapter inside retrofit

In a Fragment, in the onCreateView method, I set the adapter in this way:

final BusStopAdapter mAdapter = new BusStopAdapter(nearbyBusStations);
mRecyclerView.setAdapter(mAdapter);

Then there is a retrofit method which set the nearbyBusStations in the adapter:

 service.getJson(origin, dest).enqueue(new Callback<DirectionResults>() {
            @Override
            public void onResponse(Call<DirectionResults> call, Response<DirectionResults> response) {
                Route routeA = response.body().getRoutes().get(0);
                Legs legs = routeA.getLegses().get(0);
                nearbyBusStations.add(new NearbyBusStation(currentItem.getName(), String.valueOf(legs.getDistance().getValue()), legs.getDuration().getText()));
            mAdapter.swapItems(nearbyBusStations);
               mAdapter.notifyItemInserted(nearbyBusStations.size() - 1);
            }

            @Override
            public void onFailure(Call<DirectionResults> call, Throwable t) {
                Log.d("CallBack", " Throwable is " + t);
            }
        });

These lines of code works, but the problem is that I want to clear the mRecyclerView in another AsyncTask. I've tried some ways but nothing works. I need to clear the mRecyclerView because I have to refresh the near bus stop station.

Upvotes: 1

Views: 654

Answers (2)

Rajesh Panchal
Rajesh Panchal

Reputation: 1170

You should clear your list and notify the adapter as below

nearbyBusStations.clear();
mAdapter.setnotifyDataSetChanged();

Upvotes: 2

Sarbjyot
Sarbjyot

Reputation: 136

Simply clear your arraylist and notify your adapter where you want to clear your recyclerview.

Example:-

nearbyBusStations.clear();
mAdapter.setnotifyDataSetChanged();

Hope its help you..

Upvotes: 2

Related Questions