androidandroid-fragmentsandroid-recyclerviewandroid-adapterandroid-adapterview

Repeating Pictures for other items in the RecyclerView


First of all, Forgive me, my English is not good.

i have a RecyclerView with 9 items, But from the fourth item to the next, Pictures of the first 4 items are repeated for other items! but other fields For all items in the RecyclerView, Have been completed correctly.

how to fix it problem?

this is my code:

My Model:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}

My Adapter:

public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}

My Fragment:

public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}

Solution

  • You should use image in integer type -> R.drawable.p2

    and your app/build.gradle add this :

    compile 'com.github.bumptech.glide:glide:4.3.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
    

    Your Model:

     public class LocationsListModel {
        public String name, review, price;
        public int imageResource;
    
        public LocationsListModel(String name, String review, String price, int imageResource) {
            this.name = name;
            this.review = review;
            this.price = price;
            this.imageResource = imageResource;
        }
    }
    

    Your Adapter:

    @Override
        public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    
            LocationsListModel locationsListModel = locationList.get(position);
    
            GlideApp.with(context).load(locationsListModel.imageResource)
                                  .into(holder.imageView);
    
            holder.locationName.setText(locationsListModel.name);
            holder.review.setText(locationsListModel.review);
            holder.locationPrice.setText(locationsListModel.price);
    
        }
    }
    

    Your Fragment

    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", R.drawable.pic1));