thanx Hammad Tariq Sahi i have used your logic and solve my problem in this way....i have also refereed this link
in my adapter
ArrayList<ViewHolder> viewHoldersList;
private Handler handler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (viewHoldersList) {
for (ViewHolder holder : viewHoldersList) {
holder.updateTimeRemaining();
}
}
}
};
inside constructor of my adapter
viewHoldersList = new ArrayList<>();
startUpdateTimer();
and added this method to calculate time
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
handler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
added two methods to my viewholder class
public void setData(Product product){
this.product = product;
}
public void updateTimeRemaining() {
if(product.getProductType().equalsIgnoreCase("Auction Product")) {
Log.e("hetal",product.getProductType());
try {
String start = product.getStart();
String stop = product.getStop();
//txt_timeleft.setText("");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar start_date = Calendar.getInstance();
start_date.setTime(format.parse(start));
Calendar end_date = Calendar.getInstance();
end_date.setTime(format.parse(stop));
final Calendar today = Calendar.getInstance();
CountDownTimer timer;
long timeDiff = end_date.getTimeInMillis() - today.getTimeInMillis();
if (timeDiff > 0) {
long seconds = timeDiff / 1000 % 60;
long minutes = timeDiff / (60 * 1000) % 60;
long hours = timeDiff / (60 * 60 * 1000) % 24;
//long days = (int) diff / (24 * 60 * 60 * 1000);
long days = TimeUnit.MILLISECONDS.toDays(timeDiff);
String left = "";
if (days > 0)
left += days + " " + context.getString(R.string.txt_day) + " ,";
if (hours > 0)
left += hours + " " + context.getString(R.string.txt_hour) + " ,";
if (minutes > 0)
left += minutes + " " + context.getString(R.string.txt_minute) + " ,";
left += seconds + " " + context.getString(R.string.txt_second);
final String finalLeft = left;
txt_timeleft.setText(finalLeft);
} else {
txt_timeleft.setText("Time Out !!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
and finally inside onBindViewHolder
synchronized (viewHoldersList) {
viewHolder.setData(product);
if(viewHoldersList.size()< (list.size()-2)) viewHoldersList.add(viewHolder);
}
works perfect....thanx all
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…