-
Countdown timer in listview android
about 8 years ago
-
about 8 years ago
Please elaborate your question. What kind of customization you want in ListView.
For countdown timer you can use the below code:- final Handler handler = new Handler(); Timer timer = new Timer(); TimerTask doAsynchronousTask = new TimerTask() { @Override public void run() { handler.post(new Runnable() { public void run() { // this line will execute in every 5 min } }); } }; timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
-
almost 9 years ago
Finally I got the solution of this problem. I have used the recyclerview instead of using Listview. From here you can download the source code of Countdown timer in listview
Here is my adapter:
package com.androidsolutionworld.multipletimer; import android.os.CountDownTimer; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; public class Adapter extends RecyclerView.Adapter{ private ArrayList al_data; public class MyViewHolder extends RecyclerView.ViewHolder{ public TextView tv_timer; CountDownTimer timer; public MyViewHolder (View view){ super(view); tv_timer = (TextView)view.findViewById(R.id.tv_timer); } } public Adapter(ArrayList al_data) { this.al_data = al_data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(final MyViewHolder holder, int position) { holder.tv_timer.setText(al_data.get(position)); if (holder.timer != null) { holder.timer.cancel(); } long timer = Long.parseLong(al_data.get(position)); timer = timer*1000; holder.timer = new CountDownTimer(timer, 1000) { public void onTick(long millisUntilFinished) { holder.tv_timer.setText("" + millisUntilFinished/1000 + " Sec"); } public void onFinish() { holder.tv_timer.setText("00:00:00"); } }.start(); } @Override public int getItemCount() { return al_data.size(); } }
-
2 Answer(s)