1

I have a RecyclerView whose adapter fetches details about a place ID using the Google Places API. The adapter then displays those details (eg. place address) in each element of the RecyclerView.

Is there a recommended way to achieve this asynchronously? In my current solution, the place addresses are sometimes displayed in the incorrect RecyclerView element when the user scrolls up or down. Also when tapping on a RecyclerView element to go to the detail screen, then going back to the original screen, some of the place addresses are missing in the RecyclerView (presumably there is a delay when the details are still being fetched from the Places API).

public void onBindViewHolder(final ViewHolder v, int position) {
    Places.GeoDataApi.getPlaceById(googleApiClient, placeID)
            .setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(PlaceBuffer places) {
                    Place place = places.get(0);

                    // Display address in the RecyclerView element
                    v.address.setText(place.getName());
                }
     }
user2181948
  • 1,646
  • 3
  • 33
  • 60

1 Answers1

3

Dont hit api in onbindviewholder method. Because onbindviewholder method execute every time when you scroll recyclerview.

This will be better if you fetch data in activity or fragment and then , store that data in arraylist and pass to recyclerview adapter.

And problem of shuffling item position is due to recyclerview recycle every item on scroll.

So in your onbindviewholder method, set this property to viewHolder

viewHolder.setIsRecyclable(false);
gprathour
  • 14,813
  • 5
  • 66
  • 90
Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24