简体   繁体   中英

Remove certain items which are empty from RecyclerView list?

I'm making an app using TMDB API and have gotten stuck at a small issue.

TMDB API shows seasons and episodes which are empty, basically, those are yet to air but since those are empty, the app shows a blank item that I'm trying to get rid of.

Here's my adapter:

public class SeasonAdapter extends RecyclerView.Adapter<SeasonAdapter.ViewHolder> {

    private final List<Season> seasons;
    private final Context context;
    private final RequestOptions requestOptions;

    public SeasonAdapter(List<Season> seasons, Context context) {
        this.seasons = seasons;
        this.context = context;
        requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.poster_placeholder).error(R.drawable.poster_placeholder);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_season_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Season season = seasons.get(position);

        holder.tvTitle.setText(season.getSeasonTitle());

        if (season.getSeasonDate() != null && !season.getSeasonDate().isEmpty()) {
            holder.tvDate.setText(context.getResources().getString(R.string.aired_on) + season.getSeasonDate());
        } else {
            holder.tvDate.setVisibility(View.GONE);
        }

        if (season.getSeasonEpisodes() == 0) {
            seasons.remove(position);
        }

        holder.tvEpisodes.setText(String.valueOf(season.getSeasonEpisodes()) + context.getResources().getString(R.string.total_episodes));
        Glide.with(context).load(season.getSeasonImageURL()).apply(requestOptions).into(holder.ivPoster);

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, EpisodeActivity.class);
            intent.putExtra("title", season.getShowTitle());
            intent.putExtra("seasonTitle", season.getSeasonTitle());
            intent.putExtra("seasonNo", season.getSeasonNo());
            intent.putExtra("tvId", season.getTvId());
            v.getContext().startActivity(intent);
        });

    }


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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivPoster;
        public TextView tvTitle, tvDate, tvEpisodes;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            ivPoster = itemView.findViewById(R.id.ivSeasonPoster);
            tvTitle = itemView.findViewById(R.id.tvSeasonTitle);
            tvDate = itemView.findViewById(R.id.tvSeasonAired);
            tvEpisodes = itemView.findViewById(R.id.tvSeasonEpisodes);

            //Poster Corners
            ivPoster.setClipToOutline(true);
        }
    }
}

I tried doing this:

if (season.getSeasonEpisodes() == 0) {
    seasons.remove(position);
}

It does seem to hide the season which has no episodes but if a show has multiple seasons without episodes, my app crashes so I figured this isn't the right solution so any help is appreciated.

I suggest performing that removal logic in the constructor of the adapter rather than in onBind . onBind happens as the recycler view is finalising the details of each view holder immediately before it's shown to the user. You want to do as little as possible logic in here to keep the recycler view performant.

Inside the constructor (or even before the list is passed in) you should perform a loop and remove those items that don't meet the criteria before setting the instance variable.

It's been a long time since I wrote code in java and so I'd end up with unhelpful incorrect syntax if I tried to do it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM