Possible Duplicate RecyclerView Mixing Up Items
I have a Recyclerview that i populate with items through an adapter and a List. The problem is, on refreshing(via SwipeRefreshLayout), data for a countdown timer mixes up and the wrong value shows up on another item as highlighted in the image below. Item 1 highlighted timer is supposed to show 'Expired' but on refresh instead it takes the timer value for item 2
Here is my onBindViewHolder code in my adapter. Am i implementing it correctly?
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedModelViewHolder> {
private List<FeedModel> feedModelList;
private MainFeedListener listener;
private Context mContext;
String cPrice;
public FeedAdapter(List<FeedModel>feedModelList, Context context, MainFeedListener bidFeedListener) {
this.feedModelList = feedModelList;
this.mContext = context;
this.listener = bidFeedListener;
}
@NonNull
@Override
public FeedModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bid_feed, parent, false);
return new FeedModelViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final FeedModelViewHolder holder, final int position) {
final FeedModel feedModel = feedModelList.get(position);
((TextView) holder.bidView.findViewById(R.id.bid_title)).setText(feedModel.getTitle());
((TextView) holder.bidView.findViewById(R.id.start_price)).setText(feedModel.getCurrency() +" "+Convert(feedModel.getStartPrice()));
((TextView) holder.bidView.findViewById(R.id.tags)).setText(feedModel.getTags());
((TextView) holder.bidView.findViewById(R.id.location)).setText(feedModel.getLocation());
new CountDownTimer(feedModel.getDeadline(), 1000) {
public void onTick(long millisUntilFinished) {
((TextView) holder.bidView.findViewById(R.id.deadline)).setText(formatMilliSecondsToTime( millisUntilFinished));
feedModel.setTime(millisUntilFinished);
}
public void onFinish() {
((TextView) holder.bidView.findViewById(R.id.deadline)).setText("EXPIRED");
}
}.start();
if (!TextUtils.isEmpty(feedModel.getImageUrl())) {
Glide.with(mContext).load(feedModel.getImageUrl())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into((ImageView) holder.bidView.findViewById(R.id.bid_thumbnail));
}
((LinearLayout) holder.bidView.findViewById(R.id.article_card_root)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onBidRowClicked(feedModel);
}
});
}
@Override
public int getItemCount() {
return feedModelList.size();
}
public static class FeedModelViewHolder extends RecyclerView.ViewHolder {
private View bidView;
public FeedModelViewHolder(View v) {
super(v);
bidView = v;
}
}
String Convert(Double d){
int i;
d +=0.005;
i= (int) (d*100);
Double b = (double) (i / 100);
return b.toString();
}
public static String formatMilliSecondsToTime(long milliseconds) {
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60))); //
// int days = (int) ((milliseconds / (1000 * 60 * 60)) % 24); //
return twoDigitString(hours) + ":" + twoDigitString(minutes) + ":"
+ twoDigitString(seconds);
}
private static String twoDigitString(long number) {
if (number == 0) {
return "00";
}
if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}
}
I populate the list using a Volley library and pass the list to the adapter through the constructor