I am developing one android application and on one screen i have Listview with countdown timer on each row. I am using List Adapter. I tried Google and other reference but not getting actual output. I have seconds returned from web-service, I am converting seconds to hh:mm:ss format and started countdown timer using CountdownTimer class. but when i am scrolling Listview it changes behavior, I know this is the common problem for android Listview but i want to display countdown timer on each row with complete moving time second by second.
Please help me friend, Thanks in advance.
meh from this thread here gave a good advice.
From within the CountDownTimer, don't update the textviews in the listview-item directly, but update some String in the data-objects from the adapter. Afterwards let another separate CountDownTimer call adapter.notifyDataSetChanged() to let the listitem-views update themselves. I call this one adapterNotifier in code.
The adapterNotifier alone should call adapter.notifyDataSetChanged() to avoid the additional overhead if every other single CountDownTimer would itself call adatper.notifyDataSetChanged().
In the getView()-method from the adapter, don't do anything fancy, only read the pre-computed strings from the data-object and update the textviews as usual.
The main drawback to this approach is, that you update the whole of every listitem, which could be overkill if you have e.g. network-loaded images in the listitems. I will try to optimize this further.
Here is some code
public Adapter(Context context, ListData dataList) {
super(context, dataList);
this.dataList = dataList;
initTimerData(dataList);
}
private void initTimerData(ListData dataList) {
long longestTimerInterval = 0;
for (final Data data : dataList) {
if (data.show_timer) {
if (data.end_time == null) {
data.show_timer = false;
continue;
}
long endTimeMillis = 0;
try {
endTimeMillis = dataDateTimeProcessor.parse(data.end_time).getTime();
} catch (ParseException e) {
Log.e(DataDetailFragment.class.getSimpleName(), "Error parsing data-time: ", e);
}
// TODO use the server-time for reliability
long nowTime = System.currentTimeMillis();
long timeInterval = endTimeMillis - nowTime;
// if the data is over or more than 24 hours away, don't display the timer
if (nowTime >= endTimeMillis ||
timeInterval > 24 * 3600 * 1000) {
data.show_timer = false;
continue;
}
// save the longest interval for an extra adapter-notifier timer
if (timeInterval > longestTimerInterval)
longestTimerInterval = timeInterval;
// this listener holds the references to the currently visible data-timerviews
DataCountDownTimerListener timerListener = new DataCountDownTimerListener() {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000) % 60);
int minutes = (int) ((millisUntilFinished / (60000)) % 60);
int hours = (int) ((millisUntilFinished / (3600000)) % 24);
data.timerSeconds = String.format("%02d", seconds);
data.timerMinutes = String.format("%02d", minutes);
data.timerHours = String.format("%02d", hours);
}
#Override
public void onFinish() {
data.timerSeconds = "00";
data.timerMinutes = "00";
data.timerHours = "00";
}
};
if (data.endTimer != null) {
Utils.stopDataTimer(data);
}
data.endTimer = new DataCountDownTimer(timeInterval, 1000);
data.endTimer.setDataCountDownTimerListener(timerListener);
data.endTimer.start();
} else {
data.show_timer = false;
}
}
initAdapterNotifier(longestTimerInterval);
}
private void initAdapterNotifier(long longestTimerInterval) {
// no timers here, go away
if (longestTimerInterval == 0)
return;
adapterNotifier = new DataCountDownTimer(longestTimerInterval, 1000);
DataCountDownTimerListener adapterNotifierListener = new DataCountDownTimerListener() {
#Override
public void onTick(long millisUntilFinished) {
// TODO evaluate performance
// update the adapter-dataset for timer-refreshes
notifyDataSetChanged();
}
#Override
public void onFinish() {
// last timer finished, nothing to do here
}
};
adapterNotifier.setDataCountDownTimerListener(adapterNotifierListener );
adapterNotifier.start();
}
public void stopDataTimers() {
if (adapterNotifier != null)
adapterNotifier.cancel();
for (Data data : dataList) {
if (data.endTimer != null) {
Utils.stopDataTimer(data);
}
}
}
you can create a class that will handle your timer in each row. I mean a reusable class. you already created a class to make your timer then utilize it.In that class, you should be able to monitor your listview item so that the timer will continue to run. you can reference the id of that listitem.
hope it gives you some hint
Related
I'm making this app that tells how much longer till we reach a destination but whenever I try to use it it just black screens just crashes. I think it has something to do with the loop but I can't find anything online Can anyone help? Thanks in advance.................................................................................................................
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Calendar;
import java.util.TimeZone;
import java.time.*;
import java.math.*;
public class TimeTIll extends AppCompatActivity {
private static final String DateUtils = null;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_till);
txt = (TextView) findViewById(R.id.TimeDipslay);
int i = 0;
while (i < 10) {
Program();
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
public void Program() {
int totalMinutesSinceMidnight = 0;
long ms = 0;
long secondsM = 0;
long SecondsTillEstimate = 0;
long SecondsTillArrival1 = 0;
double MinutesTillArrival = 0;
double hoursTillArrival = 0;
double SecondsTillArrival = 0;
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.set(Calendar.HOUR_OF_DAY, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);
midnight.set(Calendar.MILLISECOND, 0);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
now = Calendar.getInstance();
midnight = Calendar.getInstance();
ms = now.getTime().getTime() - midnight.getTime().getTime();
totalMinutesSinceMidnight = (int) (ms / 1000);
secondsM = totalMinutesSinceMidnight;
SecondsTillEstimate = (60 * 60 * 23);
SecondsTillArrival1 = SecondsTillEstimate - secondsM;
hoursTillArrival = Math.floor(SecondsTillArrival1 / 3600);
SecondsTillArrival1 -= (hoursTillArrival * 3600);
MinutesTillArrival = Math.floor(SecondsTillArrival1 / 60);
SecondsTillArrival1 -= (MinutesTillArrival * 60);
SecondsTillArrival = SecondsTillArrival1;
txt.setText((int) hoursTillArrival + " Hours " + (int) MinutesTillArrival + " Minutes " + (int) SecondsTillArrival + " Seconds Till Arrival In Floriada");
}
}
Please read about the activity life-cycle. You cannot call long-running tasks in onCreate and expect them to show anything other than an ANR crash (your onCreate method blocks the UI thread with all those sleep statements and generates an ANR crash before it even gets to the onStart and onResume stages where it actually shows the activity).
If you want the UI to change dynamically, you could use a handler to execute a method at a regular interval. For example, to update something at a regular interval (1000 ms here) you could do:
public class MainActivity extends AppCompatActivity {
private final Handler handler = new Handler();
private TextView txt;
private int count = 0;
private final int updateFreqMs = 1000; // call update every 1000 ms
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onResume() {
super.onResume();
handler.postDelayed(new Runnable() {
#Override
public void run() {
updateTime();
handler.postDelayed(this, updateFreqMs);
}
}, updateFreqMs);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_till);
txt = findViewById(R.id.TimeDipslay);
}
private void updateTime() {
// here you update the displayed value
// this will be called every second indefinitely
// do your math and generate a string to print, just showing
// a counter here to demonstrate
++count;
txt.setText("Count = " + count);
}
}
I think that the problem is that you are trying to make sleep the main thread of the application. In Android I knew that for security reasons this can't be done.
(Look this https://www.codementor.io/tips/0743378261/non-freezing-sleep-in-android-app for details )
Try this solution instead:
How to pause / sleep thread or process in Android?
You Shouldn`t use it inside main thread specially when you are doing operation inside it.
You may have to use some thing like that.
viewModelScope.launch(Dispatchers.IO) {
while (statues==true){
if (statue==true){
delay(500)
//ToDo
}
}
}
what I'm trying to do:
My app connects with a Bluetooth accelerometer and gets acceleration data every 50ms. I want to check for these with a Runnable. And this worked fine. However, for the complete app I added two CountDownTimers:
1) introTimer(), which runs in an AlertDialog and counts down from 3. OnFinish() calls (i) my Runnable, that deals with new acceleration data and (ii) a second timer called
2) mainTimer(), which counts down from 20 and refreshes a TextView on the UI.
My problem is, that my Runnable is only executed once when it is first called. Why is that? Apparently I have not yet understood the threading principles behind this. My next step (probably going to be complicated) is to refresh ImageViews every 50ms (or maybe less often, but still with a relatively high frequency) on the UI, depending on the accelerometer data.
I have thought about deleting the Runnable and putting my calculations in the CountDownTimer, but this might be difficult or inaccurate with the timing/frequency/periods. What do you think?
Here are the code parts. If you think other parts are relevant, I can post them as well:
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View promptView = layoutInflater.inflate(R.layout.intro_timer_prompt, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final TextView introTimer = (TextView) promptView.findViewById(R.id.introTimer);
builder.setView(promptView);
AlertDialog alertD = builder.create();
alertD.show();
introTimer(introTimer, alertD);
}
private void introTimer(final TextView introTimer, final AlertDialog alertD) {
new CountDownTimer(4*1000, 1000) {
public void onTick(long millisUntilFinished) {
introTimer.setText( ""+ (millisUntilFinished / 1000));
}
public void onFinish() {
alertD.cancel();
// mainTimer();
clearArrayLists();
mHandler = new Handler();
mHandlerTask.run();
mainTimer();
}
}.start();
}
private void mainTimer() {
new CountDownTimer((int)DURATION*1000, 10) {
public void onTick(long millisUntilFinished) {
//mTimerView.setText("seconds remaining: " + millisUntilFinished / 1000 + ":" + millisUntilFinished / 1000. );
long millisecs = Math.round(millisUntilFinished % 1000 / 100);
mTimerView.setText("seconds remaining: " + millisUntilFinished / 1000 + ":" + millisecs );
}
public void onFinish() {
String energy = new DecimalFormat("#.##").format(accumulatedKineticEnergy.get(accumulatedKineticEnergy.size()-1));
mTimerView.setText("Accumulated Energy is " + energy + " Joule.");
}
}.start();
}
Runnable mHandlerTask = new Runnable() {
#Override
public void run() {
Log.i(TAG, "executed only once :( ");
if (a_x.size() < DURATION/INTERVAL) {
try {
calcAccList();
//...calc things...;
mHandler.postDelayed(this, (long) (1000*INTERVAL));
} catch (Exception e) {
e.getMessage();
}
}
else {
mHandler.removeCallbacks(mHandlerTask);
}
}
};
A countdown timer doesn't repeat itself when it expires, but you need to start it again. Maybe you can use the AlarmManager in this case to have a better design.
My intention is to create an timer which starts from 00:00:00 and hangs together with a recording function. This recording is done in a service so, the timer is also in the same service. If the app moves to the background, the recording and thus the timer keep on going and the app picks the time up at the onResume with a myService.getTime().
But I experience two strange things. The first is that my timer sometimes goed faster than a second, sometimes slower, sometimes jumps from for example 00:00:04 to 00:00:06 etc. There is no consitency in it. I use the code below, but there might be a better option to solve this? The second is that it is causing a lag on my button, although I am starting it in a service?
SERVICE
//////////TIMER FUNCTION START//////////
private void startTimerClick() {
if (stopped) {
startTime = System.currentTimeMillis() - elapsedTime;
} else {
startTime = System.currentTimeMillis();
}
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
}
private void pauseTimerClick() {
mHandler.removeCallbacks(startTimer);
stopped = true;
}
private void stopTimerClick() {
mHandler.removeCallbacks(startTimer);
stopped = false;
}
private void startTimer() {
startTimer = new Runnable() {
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
updateTimer(elapsedTime);
mHandler.postDelayed(this, REFRESH_RATE);
}
};
}
private void updateTimer(float time) {
secs = (long) (time / 1000);
mins = (long) ((time / 1000) / 60);
hrs = (long) (((time / 1000) / 60) / 60);
/*
* Convert the seconds to String and format to ensure it has a leading
* zero when required
*/
secs = secs % 60;
seconds = String.valueOf(secs);
if (secs == 0) {
seconds = "00";
}
if (secs < 10 && secs > 0) {
seconds = "0" + seconds;
}
/* Convert the minutes to String and format the String */
mins = mins % 60;
minutes = String.valueOf(mins);
if (mins == 0) {
minutes = "00";
}
if (mins < 10 && mins > 0) {
minutes = "0" + minutes;
}
/* Convert the hours to String and format the String */
hours = String.valueOf(hrs);
if (hrs == 0) {
hours = "00";
}
if (hrs < 10 && hrs > 0) {
hours = "0" + hours;
}
}
//////////TIMER FUNCTION END////////////
public String getHours(){
return hours;
}
public String getMinutes(){
return minutes;
}
public String getSeconds(){
return seconds;
}
}
ACTIVITY(/FRAGMENT)
private void timerStart() {
handler = new Handler();
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
handler.post(new Runnable() {
#Override
public void run() {
timer.setText(myService.getHours()+":"+myService.getMinutes()+":"+myService.getSeconds());
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
You are using Threads both in your Service and in your Activity/Fragment code.
Using threads for time sensitive tasks in Android is a problem because Android is able to significantly delay threads.
I have been using a ScheduledThreadPoolExecutor for a similar task and it worked great.
You are then using it like this:
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); // where 1 is the number of needed concurrent threads. 1 should last for your needs.
executor.scheduleWithFixedDelay(new TimerTask() {
// your recurringly executed code here
}, 0, 1, TimeUnit.SECONDS);
Basically I am doing a cardio feature and have three countdown timers in a row nested within each other, so when one timer finishes, the next one starts. One for preparation time, one for workout time and one for rest time, the user chooses the times of these.
I need it to loop however many times the user selects from a numberpicker, but no matter what I do it only goes through it once and doesn't loop so I know it all works it's just the looping part that doesn't work.
Am I missing something here? Is there a better way to do this?
//Main countdown timers loop
for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
{
prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= (6 * 1000))
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvCountDown.setTextColor(Color.GREEN);
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= 6 * 1000)
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvCountDown.setTextColor(Color.GREEN);
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= 6 * 1000)
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
roundCount = roundCount + 1;
}
}.start();
}
}.start();
}
}.start();
}
the issue here is that you create prepCountTimer and assign on finish ect, then start it. then it reaches the end of for each and loops again making another preopCountTimer and starting it. you need to make your restCountTimer start the next preopCountTimer once it's done. unless I'm understanding something wrong here.
public void callingMethod() {
timerMethod(times.getValue());
// execution continues as your timer will run in a different thread
}
public void timerMethod(final int count) {
if (count == 0) {
// we have done the number of timers we want we can
// call whatever we wanted to once our timers were done
}
//you could use count to get the times for each timer here
startTimer(_finalPrep, new timerListner() {
#Override
public void timerFinish() {
//when timer 1 finishes we will start timer 2
startTimer(_finalWorkout, new timerListner() {
#Override
public void timerFinish() {
//when timer 2 finishes we will start timer 3
startTimer(_finalRest, new timerListner() {
#Override
public void timerFinish() {
//when timer 3 finishes we want to call the next timer in the list.
timerMethod(count - 1);
}
});
}
});
}
});
}
private interface timerListner {
void timerFinish();
}
public void startTimer(int timerTime, final timerListner onFinish) {
// you can pass in other parameters unqiue to each timer to this method aswell
CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if (millisUntilFinished <= (6 * 1000)) {
tvCountDown.setTextColor(Color.RED);
}
}
#Override
public void onFinish() {
onFinish.timerFinish();
}
};
timer.start();
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public class MainActivity extends Activity {
private Button Start, Reset, Stop;
private EditText stop_watch, lblDate, lblTime;
public MainActivity() {
init();
}
private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Start = (Button)findViewById(R.id.button1);
Stop = (Button)findViewById(R.id.button3);
Reset = (Button)findViewById(R.id.button2);
stop_watch = (EditText)findViewById(R.id.editText3);
lblTime = (EditText)findViewById(R.id.editText2);
lblDate = (EditText)findViewById(R.id.editText1);
Start.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startactionPerformed();
}
});
Stop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopactionPerformed();
}
});
Reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
resetactionPerformed();
}
});
}
public void init() {
swThread.setDaemon(true);
ucThread.setDaemon(true);
swThread.start();
ucThread.start();
}
/** Listens to the Start/Stop/Resume button. */
void startactionPerformed() {
swThread.go();
//init();
}
void stopactionPerformed() {
swThread.noGo();
}
void resetactionPerformed() {
swThread.reset();
}
/** A thread that updates the current date & time. */
private class UpdateClockThread extends Thread {
/** The actual work of the thread. */
public void run() {
while (true) {
lblTime.setText("ampm");
Calendar now = Calendar.getInstance();
String month = Integer.toString(now.get(Calendar.MONTH)+1);
String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
String year = Integer.toString(now.get(Calendar.YEAR));
String hour = Integer.toString(now.get(Calendar.HOUR));
if (hour.equals("0")) hour = "12";
String minute = Integer.toString(now.get(Calendar.MINUTE));
if (minute.length() == 1) minute = "0" + minute;
String second = Integer.toString(now.get(Calendar.SECOND));
if (second.length() == 1) second = "0" + second;
String ampm = now.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
lblDate.setText(month + "/" + date + "/" + year);
lblTime.setText(hour + ":" + minute + ":" + second + " " + ampm);
try {
sleep(500);
} catch (InterruptedException e) {}
}
}
}
/** A thread that keeps track of the stop watch & updates
* the display accordingly.
*/
class StopwatchThread extends Thread {
/** Whether or not stop watch is running. */
private boolean going = false;
/** Stores elapsed milliseconds of previous runs. */
private long prevElapsed = 0;
/** Stores beginning time of this run. */
private Date startDate = new Date();
/** Returns elapsed time in milliseconds.
*#return The elapsed time
*/
private long elapsedTime() {
return prevElapsed + (going ? new Date().getTime() - startDate.getTime() : 0);
}
/** Changes the number of elapsed milliseconds into a string.
*#param time Number of elapsed milliseconds
*#return The elapsed time as a string.
*/
private String msToString(long time) {
String ms, sec, min;
if (time % 10 >= 5) //round to nearest hundredth
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() < 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
return min + ":" + sec + "." + ms;
}
/** Called when the stop watch is to go.
*/
public void go() {
startDate = new Date();
going = true;
}
/** Called when the stop watch is to stop.
*/
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
/** Resets the stop watch.
*/
public void reset() {
going = false;
prevElapsed = 0;
}
/** Adds a lap to the list.
*/
/** Main code of the thread.
*/
public void run() {
while (true) {
stop_watch.setText(msToString(elapsedTime()));
yield();
}
}
}
}
What is my app getting force close? There is some error with the thread. But i'm not able to figure it out?
Try make all interaction with UI from threads in runOnUiThread method of Activity http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29
EDIT
private class UpdateClockThread extends Thread {
public void run() {
/* Do somthing expensive */
......
/* Update UI */
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
/* Do UI update */;
lblTime.setText("ampm");
......
}
});
}
}
You should run adb logcat on your developer machine. Usually if you get a force close a traceback is displayed in the log.
Your class StopwatchThread contains the line:
stop_watch.setText(msToString(elapsedTime()));
This will not work, and neither will the UI updates within UpdateClockThread. This is because you cannot modify the UI from a background thread. Your background thread should either:
send a message to a Handler that has been created on the UI thread, so that the Handler then makes the update to the UI, or
you can pass a runnable into runOnUiThread that performs the UI update.