Search code examples
javaandroidandroid-recyclerviewandroid-adapterboolean-expression

Displaying values in recyclerview (based on boolean value) returns same result for all items


I have a recyclerview with a lot of items. I use adapter to populate the recyclerview from server, i.e. name, date, time, and so on, and it works OK.

Only issue I have is when I try and populate it based on a boolean value. When I try to add image star (for favourites option), and when I try to set is closed text (for active option), I always get all items with the "favourite icon" and "is closed" message.

For example, what I am trying to do when the working hours are returning active as false is like this:

if (!restaurant.getActive()) {
    holder.isClosed.setVisibility(View.VISIBLE);
}

Boolean works just fine when I am setting it from a RestaurantDescriptionActivity, it saves and updates on server as it should, and it also returns the correct value, so I am not sure where my mistake could be.

I am returning values for boolean like this in my RestaurantModel:

@SerializedName("favourites")
private boolean isFavourite;
@SerializedName("active")
private boolean active;
----
public void writeToParcel(Parcel parcel, int i) {

    parcel.writeString(thumbnailUrl);
    parcel.writeString(dateTime);
    parcel.writeString(id);
    parcel.writeValue(comments_enabled);
    // I TRIED WRITING BOOLEAN TO PARCEL IN TWO WAYS, AS SHOWN BELOW
    parcel.writeValue(isFavourite);
    parcel.writeInt(active ? 1 : 0);
}

//// UPDATED PART
protected restaurant(Parcel in) {
   ...
    isFavourite = (Boolean) in.readValue(Boolean.class.getClassLoader());
    active = Boolean.parseBoolean(in.readString());   
}
//// UPDATED PART

public boolean getActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

Adapter:

class restaurantsViewHolder extends RecyclerView.ViewHolder {
    public TextView restaurantName, date, time, isClosed;
    ImageView thumbNail, isFavourite;

    restaurantsViewHolder(View itemView) {
        super(itemView);

        thumbNail = itemView.findViewById(R.id.thumbnail);
        isFavourite = itemView.findViewById(R.id.imageViewIsFavourite);
        restaurantName= itemView.findViewById(R.id.restaurantNameIcon);
        date = itemView.findViewById(R.id.date);
        time = itemView.findViewById(R.id.time);
        isClosed= itemView.findViewById(R.id.restaurantIsClosed);
    }
}

public restaurantsAdapter(List<restaurant> restaurantList, Context context) {
    this.restaurantItems = restaurantList;
    this.context = context;
}

@Override
public restaurantsAdapter.restaurantsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_restaurant, parent, false);

    return new restaurantsViewHolder(itemView);
}

@Override
public void onBindViewHolder(restaurantsAdapter.restaurantsViewHolder holder, int position) {
    // getting restaurant data for the row
    restaurant restaurant = restaurantItems.get(position);

    holder.restaurantName.setText(restaurant.getrestaurantName());
    LocalDateTime ldt = LocalDateTime.parse(restaurant.getDateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    String formattedDate = ldt.format(getLocalizedDateFormatter(Locale.getDefault()));

    holder.date.setText(formattedDate);
    //holder.date.setText(convertDate(restaurant.getDateTime())); //string dobiti u formatu, pretvoriti ga u localized i podijeliti na dva dijela
    holder.time.setText(convertTime(restaurant.getDateTime()));
    holder.isFavourite.clearColorFilter();
    if (!restaurant.getActive()) {
        holder.isClosed.setVisibility(View.GONE);
    }
    if (restaurant.getIsFavourite()) {
        holder.isFavourite.setImageResource(R.drawable.ic_icon_star_ppdcolor);
    }

}

Solution

  • You need to code else part inside RecyclerView item otherwise it will repopulate its previous view state.

    if (!restaurant.getActive()) {
        holder.isClosed.setVisibility(View.GONE);
    } else{
        holder.isClosed.setVisibility(View.VISIBLE);
    }
    
    if (restaurant.getIsFavourite()) {
        holder.isFavourite.setImageResource(R.drawable.ic_icon_star_ppdcolor);
    } else{
        holder.isFavourite.setImageResource(R.drawable.your_icon_when_not_favourite);
    }