I have a recycler view where each item has a countdown timer and there might be a chance that multiple countdown timers end at the same time. What i want to do is to remove the items when the timer ends. If there is only one item then the code works absolutely fine but when there are more than one then it crashes with indexoutofbounds exception. I tried different solutions mentioned on stack overflow but none of them seem to help me with removing the view.
Here is my adapter code.
public class UpcomingContestRecyclerAdapter extends RecyclerView.Adapter<UpcomingContestRecyclerAdapter.ViewHolder> {
private Context context;
private ArrayList<LiveAndUpcomingContest> live_and_upcoming_contest;
private Activity activity;
//Constructor
public UpcomingContestRecyclerAdapter(Activity context, ArrayList<LiveAndUpcomingContest> live_and_upcoming_contest) {
// TODO Auto-generated constructor stub
this.context = context;
this.live_and_upcoming_contest = live_and_upcoming_contest;
this.activity = context;
}
@Override
public UpcomingContestRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.upcoming_contests_list_item, viewGroup, false);
return new ViewHolder(view);
}
// Initialize the viewholder class.
public class ViewHolder extends RecyclerView.ViewHolder{
CountDownTimer timer;
private TextView game_countdown;
public ViewHolder(View view) {
super(view);
game_countdown = view.findViewById(R.id.game_countdown);
}
}
@Override
public void onBindViewHolder(final UpcomingContestRecyclerAdapter.ViewHolder viewHolder, final int position) {
if(viewHolder.timer!= null)
{
viewHolder.timer.cancel();
}
//Start countdown timer on the game countdown view.
viewHolder.timer = new CountDownTimer(live_and_upcoming_contest.get(position).getStart_time(), 500) {
public void onTick(long millisUntilFinished) {
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long elapsedHours = millisUntilFinished / hoursInMilli;
millisUntilFinished = millisUntilFinished % hoursInMilli;
long elapsedMinutes = millisUntilFinished / minutesInMilli;
millisUntilFinished = millisUntilFinished % minutesInMilli;
long elapsedSeconds = millisUntilFinished / secondsInMilli;
String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
viewHolder.game_countdown.setText(yy);
}
public void onFinish() {
viewHolder.game_countdown.setText("00:00:00");
live_and_upcoming_contest.remove(position);
notifyDataSetChanged();
}
}.start();
}
This is probably not the right way to do it because i should not be starting a new timer everytime onBindViewHolder i guess. I found the code on stack overflow to cancel the viewholder timer onBindViewHolder before starting a new one but that does not work. I tried one more solution using handler and that worked fine as a timer but even in that one when it came to removing the item, it crashed. Just to clarify once again, there will be multiple timers ending at the same time and thus multiple list items being removed at the same time, the code should be able to handle that.
Any help is appreciated. Thanks.