First three characters missing from string when send from android via bluetooth - java

When I send "M" String to the Device I call time function from where I make my String.
Code:
` mManButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
man = 1;
clearScreen();
mManButton.setBackgroundResource(R.color.button_pressed);
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mLinearButtton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
// Send a message using content of the edit text widget
sendMessage("M");
time();
}
});`
Then the time() function is called.
Here if my day is Monday then the variable day is set to 1.
That means in this function I am creating a String which has Date Format values in it. This string starts from "A" and ends with "B".
Code :
private void time()
{
int day = 0;
Date now = new Date();
String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);
switch(sdf){
case ("Monday"):
day = 1;
break;
case("Tuesday"):
day = 2;
break;
case ("Wednesday"):
day = 3;
break;
case ("Thursday"):
day = 4;
break;
case("Friday"):
day = 5;
break;
case ("Saturday"):
day = 6;
break;
case("Sunday"):
day = 7;
break;
}
int mm = Calendar.getInstance().get(Calendar.MINUTE);
int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
int yy = Calendar.getInstance().get(Calendar.YEAR)%100;
if(mm<10) {
String time1 = "A" + "0" + mm + HH + "0" + day + dd + MM + yy + "B"; //suppose time1 = A041303211216B
tv7.setText("Please Wait..");
int p = 0;
while (p < time1.length())
{
char zx = time1.charAt(p);
String xz = String.valueOf(zx);
sendMessage(xz);
p++;
}
}
else if(mm>=10) {
String time2 = "A" + mm + HH + "0" + day + dd + MM + yy + "B"; **//suppose time2 = A151303211216B**
tv7.setText("Please Wait..");
int k = 0;
while (k < time2.length())
{
char zx = time2.charAt(k);
String xz = String.valueOf(zx);
sendMessage(xz);
k++;
}
}
}
When the string is created I send each characters of the string to sendMessage().
Code :
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() !=
com.example.hasani.bluetoothterminal.BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mManButton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
}
}
The write function.
Code :
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
The wite in ConnectedThread
Code :
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(com.example.hasani.bluetoothterminal.Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
As there is a role of Handler in it.
The issue is when debugging step by step, each character is sent to the other Device and that device receives each and every string from "A" to "B", thus there is no problem.
But when i run My android app, after sending "M", the time() function is called and the String is sent but the first three characters of the string i.e; "Amm" is not received by the device.
I still don't understand what is causing the problem.
Please Help!. Will be appreciated. Thank You!

Ohkay wait!!! I got the solution. In case if someone go through the same kind of situation.
In my onClickListener I call my time() function after a 5 second delay using a second handler.
My onClickListener code is :
mManButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
man = 1;
linear = 0;
auto = 0;
cal = 0;
test = 0;
linear = 0;
clearScreen();
mManButton.setBackgroundResource(R.color.button_pressed);
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mLinearButtton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
// Send a message using content of the edit text widget
sendMessage("M");
tv7.setText("Please wait....");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
time();
}
},5000);
}
});
My time() function is :
private void time() {
int day = 0;
Date now = new Date();
String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);
switch (sdf) {
case ("Monday"):
day = 1;
break;
case ("Tuesday"):
day = 2;
break;
case ("Wednesday"):
day = 3;
break;
case ("Thursday"):
day = 4;
break;
case ("Friday"):
day = 5;
break;
case ("Saturday"):
day = 6;
break;
case ("Sunday"):
day = 7;
break;
}
int mm = Calendar.getInstance().get(Calendar.MINUTE);
int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
int yy = Calendar.getInstance().get(Calendar.YEAR)%100;
String A = "A";
String min = String.format("%02d",mm);
String hour = String.format("%02d",HH);
String d = String.format("%02d",day);
String date = String.format("%02d",dd);
String month = String.format("%02d",MM);
String year = String.format("%02d",yy);
String B = "B";
String time2 = A+min+hour+d+date+month+year+B;
sendMessage(time2);
}
Now i can receive correct data as I required. My application works like a charm.

To expand on my comment, the simplest way to handle waiting to connect would be thus. First, return success or failure from write():
public boolean write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return false;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
return true;
}
Then in sendMessage(), replace mChatService.write(send); with:
while (!mChatService.write(send))
{
try {
Thread.sleep(10);
}
catch(InterruptedException ex) {
// Uncomment to just give up
//break;
}
}
This will wait another 10 milliseconds or so before trying to resend.
There's of course a lot of improvements to make like only allowing a few retries before giving up completely, etc.
On a related note, with the above change you can probably do:
sendMessage(time1);
instead of doing it a character at a time.
Finally, your time formatting can be simplified to:
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("mmHHcddMMYY");
String time = "A" + format1.format(date) + "B";
Or something like that. See this page for details.

Related

How can I increment the variable more than one time when sensor event changed?

As you can see that I am trying to increment the variable 'c' when the accelerometer z-axis value greater than 12. But I can do it at one time, it will change the value 0 to 1 after executing the program. But I want to collect how many times the z-axis value becomes more than 12.
#Override
public void onSensorChanged (SensorEvent event) {
textView.setText(event.values[0] + "");
textView1.setText(event.values[1] + "");
textView2.setText(event.values[2] + "");
String s = new String();
s = textView2.getText().toString().trim();
Float t = Float.parseFloat(s);
int c = 0;
if (t > 11) {
c++;
txt.setText(Integer.toString(c));
}
}
int counter = 0;
#Override
public void onSensorChanged (SensorEvent event) {
textView.setText(event.values[0] + "");
textView1.setText(event.values[1] + "");
textView2.setText(event.values[2] + "");
String s = new String();
s = textView2.getText().toString().trim();
Float t = Float.parseFloat(s);
int c = 0; // ???
if (t > 11) {
c++;
counter++;
txt.setText(Integer.toString(c));
System.out.println("I need to learn how to use global
variables.\n
also the thing has been greater than \"12\"
"+counter" times."
);
}
}
Also maybe using more meaningful variable names other than "textView#" would make it less of a pain for people to figure out what you're trying to do.
You can define the variable c as a field member like below:
public class MainActivity {
private int c = 0;
(...)
#Override
public void onSensorChanged (SensorEvent event) {
textView.setText(event.values[0] + "");
textView1.setText(event.values[1] + "");
textView2.setText(event.values[2] + "");
String s = new String();
s = textView2.getText().toString().trim();
Float t = Float.parseFloat(s);
if (t > 11) {
c++;
txt.setText(Integer.toString(c));
}
}
}

Test the current time does it match between two values?

I want to test current time.
Is it the same between the two times?
-With the note, I've already done a job
function to enter the start time
and the time to end.
Now i want to complete the special function.
i'm testing this entry value between the two times.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int hourStart = 5;
int minuteStart = 30;
String PMAMStart="AM";
int hourEnd = 6;
int minuteEnd = 30;
String PMAMEnd="AM";
boolean checkTime= checkCurrentTimeBetweenTowVlue(hourStart,minuteStart,PMAMStart,hourEnd,minuteEnd,PMAMEnd);
}
private boolean checkCurrentTimeBetweenTowVlue(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){
Calendar cal = Calendar.getInstance();
return false;
}
}
In your case, you can set the individual calendar fields such as AM_PM, Hour, and Minute for both start and end dates, convert the time to minutes, and then find the difference between the times. Here is the solution:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int hourStart = 5;
int minuteStart = 30;
String PMAMStart="AM";
int hourEnd = 6;
int minuteEnd = 30;
String PMAMEnd="AM";
boolean checkTime= checkCurrentTimeBetweenTwoValues(hourStart,minuteStart,PMAMStart,hourEnd,minuteEnd,PMAMEnd);
if (checkTime) {
System.out.println("Time is Same");
} else {
System.out.println("Time is not Same");
}
}
private boolean checkCurrentTimeBetweenTwoValues(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
if (ampmStart.equalsIgnoreCase("AM")) {
startCalendar.set(Calendar.AM_PM, 0);
} else {
startCalendar.set(Calendar.AM_PM, 1);
}
startCalendar.setTimeZone(TimeZone.getDefault());
startCalendar.set(Calendar.HOUR, hStart);
startCalendar.set(Calendar.MINUTE, mStart);
if (ampmEnd.equalsIgnoreCase("AM")) {
endCalendar.set(Calendar.AM_PM, 0);
} else {
endCalendar.set(Calendar.AM_PM, 1);
}
endCalendar.setTimeZone(TimeZone.getDefault());
endCalendar.set(Calendar.HOUR, hEnd);
endCalendar.set(Calendar.MINUTE, mEnd);
long timeDifference = TimeUnit.MILLISECONDS.toMinutes(
Math.abs(endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()));
System.out.println(("Time Difference: " + timeDifference));
return (timeDifference == 0);
}
}
You can also use TimeUnit.MILLISECONDS.toSeconds to check for Seconds but for that, you need to set the Seconds component of the Calendar instance.
Regards,
AJ
If you can use Java 8 (beginning with API level 26 in Android) or if you are willing / able / allowed to import a very useful library that enables java.time support to lower API levels, then the following might be a neat solution:
public static boolean isNowBetween(int startHour, int startMinute, int endHour, int endMinute) {
LocalTime start = LocalTime.of(startHour, startMinute);
LocalTime end = LocalTime.of(endHour, endMinute);
LocalTime now = LocalTime.now();
return now.isAfter(start) && now.isBefore(end);
}
Get current minutes and hours from cal object. Convert start, end, current time into minutes, then it will be easy to compare.
private boolean checkCurrentTimeBetweenTowVlue(int hStart,int mStart,String ampmStart,int hEnd,int mEnd,String ampmEnd){
Calendar cal = Calendar.getInstance();
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
if(ampmStart.equals("PM")){
hStart += 12;
}
int minutes_start = hStart*60 + mStart;
if(ampmEnd.equals("PM")){
hEnd += 12;
}
int minutes_end = hEnd*60 + mEnd;
int minutes_curr = hours*60 + minutes;
if(minutes_curr>minutes_start && minutes_curr<minutes_end){
return true;
}
return false;
}

Saving serial data in Processing

Hello all for the second time,
Initially I was looking for a broad answer, but this thread got blocked for being "too broad"... so I've got no choice, but to go into detail. My apologies if asking the question again is against the forum guidelines, I'm new to stackoverflow so please be kind.
I’ve got data coming into a serial port at 250Hz and I’d like to save it all to a .csv file. Of course draw() is not going to be able to keep up with that rate of data...
At the moment I am using the serialEvent(port) to collect and parse the data. Once parsed out, I'm calling a function in draw to add the data to a new line in a table and then saving that table every 5 seconds...
Yes, I see the obvious flaw that if I'm saving the current data in draw then of course it's not going to be able to save all the data coming in, but rather just the data that happens to be present when the data saving function is called... but I'm not sure of the best way to solve that. A buffer scheme? Or can I have a separate thread that just adds ALL data coming in to a table?
which lead to my initial (broad) question...
Is there a way to save all incoming data to a file without polling?
Thanks to all in advance.. code below:
Twain
import processing.serial.*;
import static javax.swing.JOptionPane.*;
Table table;
String Path = "PathProvidedHere.csv";
String message;
//Some time keeping variables
int hours, minutes, seconds, milliseconds;
float SaveTime;
//Serial port selection
Serial myPort;
String COMx, COMlist = "";
final boolean debug = true;
String portName;
// Data variables
float yaw = 0.0; float pitch = 0.0; float roll = 0.0;
float A1, A2, A3, A4;
float E1, E2, E3, E4;
void setup()
{
//Set up GIU box
size(1024, 768, P3D);
frameRate(250);
smooth();
//Some other setups like fonts, graphs, etc.
//Set up the logging table
table = new Table();
table.addColumn("A1"); table.addColumn("A2"); table.addColumn("A3"); table.addColumn("A4");
table.addColumn(""); table.addColumn("E1"); table.addColumn("E3"); table.addColumn("E4");
table.addColumn(" "); table.addColumn("min"); table.addColumn("sec"); table.addColumn("milli");
portName = chooseCOM();
delay(1000);
}
void draw()
{
SavetoCSV();
//serialEvent(myPort); // read and parse incoming serial message
ACouple();
Unrelated();
FunctionsHere();
if(millis() - SaveTime > 5000)
{
saveTable(table, Path);
SaveTime=millis();
}
}
String chooseCOM()
{
setupP2 = true;
try
{
if (debug) printArray(Serial.list());
int i = Serial.list().length;
if (i != 0)
{
if (i >= 2)
{
// need to check which port the inst uses -
// for now we'll just let the user decide
for (int j = 0; j < i; )
{
COMlist += char(j+'a') + " = " + Serial.list()[j];
if (++j < i) COMlist += ", ";
}
COMx = showInputDialog("Which COM port is correct? (a,b,..):\n"+COMlist);
if (COMx == null) exit();
if (COMx.isEmpty()) exit();
i = int(COMx.toLowerCase().charAt(0) - 'a') + 1;
}
String portName = Serial.list()[i-1];
if (debug) //println(portName + " Selected");
myPort = new Serial(this, portName, 115200); // change baud rate to your liking
myPort.bufferUntil(13); // buffer until CR/LF appears, but not required..
return portName;
}
else
{
showMessageDialog(frame, "Device is not connected to the PC");
exit();
}
}
catch (Exception e)
{ //Print the type of error
showMessageDialog(frame, "COM port is not available (may\nbe in use by another program)");
//println("Error:", e);
exit();
}
return "noPort";
}
void serialEvent(Serial myPort)
{
int newLine = 13; // new line character in ASCII
do
{
message = myPort.readStringUntil(newLine); // read from port until new line
if (message != null)
{
String[] list = split(trim(message), " ");
if (list.length == 4 && list[0].equals("i"))
{
yaw = float(list[1]); // convert to float yaw
pitch = float(list[2]); // convert to float pitch
roll = float(list[3]); // convert to float roll
}
else if (list.length == 5 && list[0].equals("s"))
{
A1 = float(list[1]);
A2 = float(list[2]);
A3 = float(list[3]);
A4 = float(list[4]);
}
else if (list.length >=2 && list[0].equals("b"))
{
Battery = int(list[1]);
}
else if (list.length >= 2 && list[0].equals("m"))
{
MACid = int(list[1]);
}
else
{
//print anything extra to console
//println(message);
}
}
} while (message != null);
}
void SavetoCSV()
{
if (A1 != 0)
{
TableRow newRow = table.addRow();
newRow.setFloat("A1", (A1));
newRow.setFloat("A2", (A2));
newRow.setFloat("A3", (A3));
newRow.setFloat("A4", (A4));
//saveTable(table, Path);
}
}
Additional info:
- Processing P3
- For the record, with the rest of my script I can get draw up to 80hz or so
- I'd be okay with saving all the data and parsing it later
Went the buffer route.... I think I'm getting close now. Unsure if I'm saving the data in the right order or if the saving process will halt the rest of the processes...
Code:
import processing.serial.*;
import static javax.swing.JOptionPane.*;
//Arrays to save the data
LinkedList<Integer> A1c = new LinkedList<Integer>();
LinkedList<Integer> A2c = new LinkedList<Integer>();
LinkedList<Integer> A3c = new LinkedList<Integer>();
LinkedList<Integer> A4c = new LinkedList<Integer>();
int bufferLength = 500;
int bufflen = 0;
//Serial port selection
Serial myPort;
String COMx, COMlist = "";
final boolean debug = true;
String portName;
// Data variables
float yaw = 0.0; float pitch = 0.0; float roll = 0.0;
float A1, A2, A3, A4;
//Data log variables
Table table;
String Path = "PathtoFile.csv";
void setup() {
//Set up GIU box
size(1024, 768, P3D);
frameRate(250);
strokeWeight(50);
smooth();
//Set up the logging table
table = new Table();
table.addColumn("A1"); table.addColumn("A2"); table.addColumn("A3"); table.addColumn("A4");
portName = chooseCOM();
}
void draw() {
//SavetoCSV now called within SerialEvent()
//SavetoCSV();
//serialEvent(myPort); // read and parse incoming serial message
Some();
Unrelated();
FunctionsHere();
}
void serialEvent(Serial myPort) {
int newLine = 13; // new line character in ASCII
do {
message = myPort.readStringUntil(newLine); // read from port until new line
if (message != null) {
String[] list = split(trim(message), " ");
if (list.length == 4 && list[0].equals("i")) {
yaw = float(list[1]); // convert to float yaw
pitch = float(list[2]); // convert to float pitch
roll = float(list[3]); // convert to float roll
} else if (list.length == 5 && list[0].equals("s")) {
A1 = float(list[1]);
A2 = float(list[2]);
A3 = float(list[3]);
A4 = float(list[4]);
if (bufflen < bufferLength) {
A1c.push(int(A1));
A2c.push(int(A2));
A3c.push(int(A3));
A4c.push(int(A4));
bufflen++;
}
else{
bufflen = 0;
SavetoCSV();
}
} else if (list.length >=2 && list[0].equals("b")) {
Battery = int(list[1]);
} else if (list.length >= 2 && list[0].equals("m")) {
MACid = int(list[1]);
} else {
//print anything extra to console
//println(message);
}
}
} while (message != null);
}
void SavetoCSV() {
if (A1 != 0) {
for (int i = bufferLength - 1; i >= 0; i--){
if (i < bufferLength){
TableRow newRow = table.addRow();
newRow.setFloat("A1", (A1c.get(i)));
newRow.setFloat("A2", (A2c.get(i)));
newRow.setFloat("A3", (A3c.get(i)));
newRow.setFloat("A4", (A4c.get(i)));
} else saveTable(table, Path);
}
}
}
String chooseCOM() {
setupP2 = true;
try {
if (debug) printArray(Serial.list());
int i = Serial.list().length;
if (i != 0) {
if (i >= 2) {
// need to check which port the inst uses -
// for now we'll just let the user decide
for (int j = 0; j < i; ) {
COMlist += char(j+'a') + " = " + Serial.list()[j];
if (++j < i) COMlist += ", ";
}
COMx = showInputDialog("Which COM port is correct? (a,b,..):\n"+COMlist);
if (COMx == null) exit();
if (COMx.isEmpty()) exit();
i = int(COMx.toLowerCase().charAt(0) - 'a') + 1;
}
String portName = Serial.list()[i-1];
if (debug) //println(portName + " Selected");
myPort = new Serial(this, portName, 115200); // change baud rate to your liking
myPort.bufferUntil(13); // buffer until CR/LF appears, but not required..
return portName;
} else {
showMessageDialog(frame, "Device is not connected to the PC");
exit();
}
}
catch (Exception e)
{ //Print the type of error
showMessageDialog(frame, "COM port is not available (may\nbe in use by another program)");
//println("Error:", e);
exit();
}
return "noPort";
}

Convert given time in String format to seconds in Android

Suppose time is given in MM:SS(ex- 02:30) OR HH:MM:SS in String format.how can we convert this time to second.
In your case, using your example you could use something like the following:
String time = "02:30"; //mm:ss
String[] units = time.split(":"); //will break the string up into an array
int minutes = Integer.parseInt(units[0]); //first element
int seconds = Integer.parseInt(units[1]); //second element
int duration = 60 * minutes + seconds; //add up our values
If you want to include hours just modify the code above and multiply hours by 3600 which is the number of seconds in an hour.
public class TimeToSeconds {
// given: mm:ss or hh:mm:ss or hhh:mm:ss, return number of seconds.
// bad input throws NumberFormatException.
// bad includes: "", null, :50, 5:-4
public static long parseTime(String str) throws NumberFormatException {
if (str == null)
throw new NumberFormatException("parseTimeString null str");
if (str.isEmpty())
throw new NumberFormatException("parseTimeString empty str");
int h = 0;
int m, s;
String units[] = str.split(":");
assert (units.length == 2 || units.length == 3);
switch (units.length) {
case 2:
// mm:ss
m = Integer.parseInt(units[0]);
s = Integer.parseInt(units[1]);
break;
case 3:
// hh:mm:ss
h = Integer.parseInt(units[0]);
m = Integer.parseInt(units[1]);
s = Integer.parseInt(units[2]);
break;
default:
throw new NumberFormatException("parseTimeString failed:" + str);
}
if (m<0 || m>60 || s<0 || s>60 || h<0)
throw new NumberFormatException("parseTimeString range error:" + str);
return h * 3600 + m * 60 + s;
}
// given time string (hours:minutes:seconds, or mm:ss, return number of seconds.
public static long parseTimeStringToSeconds(String str) {
try {
return parseTime(str);
} catch (NumberFormatException nfe) {
return 0;
}
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class TimeToSecondsTest {
#Test
public void parseTimeStringToSeconds() {
assertEquals(TimeToSeconds.parseTimeStringToSeconds("1:00"), 60);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("00:55"), 55);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("5:55"), 5 * 60 + 55);
assertEquals(TimeToSeconds.parseTimeStringToSeconds(""), 0);
assertEquals(TimeToSeconds.parseTimeStringToSeconds("6:01:05"), 6 * 3600 + 1*60 + 5);
}
#Test
public void parseTime() {
// make sure all these tests fail.
String fails[] = {null, "", "abc", ":::", "A:B:C", "1:2:3:4", "1:99", "1:99:05", ":50", "-4:32", "-99:-2:4", "2.2:30"};
for (String t: fails)
{
try {
long seconds = TimeToSeconds.parseTime(t);
assertFalse("FAIL: Expected failure:"+t+" got "+seconds, true);
} catch (NumberFormatException nfe)
{
assertNotNull(nfe);
assertTrue(nfe instanceof NumberFormatException);
// expected this nfe.
}
}
}
}
int v = 0;
for (var x: t.split(":")) {
v = v * 60 + new Byte(x);
}
This snippet should support HH:MM:SS (v would result in seconds) or HH:MM (v would be in minutes)
try this
hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;
timeString = String.format("%02d",seconds);
private static final String TIME_FORMAT = "hh:mm a";//give whatever format you want.
//Function calling
long timeInMillis = TimeUtils.getCurrentTimeInMillis("04:21 PM");
long seconds = timeInMillis/1000;
//Util Function
public static long getCurrentTimeInMillis(String time) {
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault());
// sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //getting exact milliseconds at GMT
// sdf.setTimeZone(TimeZone.getDefault());
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
I have written an extension function in Kotlin for converting String to seconds
fun String?.converTimeToSeconds(): Int {
if (this.isNullOrEmpty().not()) {
val units = this?.split(":")?.toTypedArray()
if (units?.isNotEmpty() == true && units.size >= 3) {
val hours = units[0].toInt()
val minutes = units[1].toInt()
val seconds = units[2].toInt()
return (3660 * hours) + (60 * minutes) + seconds
}
}
return 0
}

Android Listview countdown timer is causing strange results

I have a listview and want to countdown each item. My issue is when I scroll the countdown times jump around to incorrect items (so it shows another items countdown time) and there is no rhyme or reason to it? here is the code
private final Map<String, TextView> mcounterlist= new HashMap<String, TextView>();
Then in my getview on my adapater I do
String gmtFormat = "yyyy-MM-dd HH:mm:ss";
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat sdf = new SimpleDateFormat(gmtFormat);
sdf.setTimeZone(tz);
Date dd = null;
try {
dd = sdf.parse(o.endAt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date now = new Date();
String sss = timeCalculate((dd.getTime() /1000)- (now.getTime() /1000) );
ea.setText("Time Left: " + sss);
TextView cdi = new TextView();
mcounterlist.put(o.id , cdi);
So at this point I I just run a repeating thread to update.
private final Runnable mRunnable = new Runnable() {
public void run() {
// TextView textView;
String gmtFormat = "yyyy-MM-dd HH:mm:ss";
TimeZone tz = TimeZone.getDefault() ;
SimpleDateFormat sdf = new SimpleDateFormat(gmtFormat);
sdf.setTimeZone(tz);
// if counters are active
if(mcounterlist.size() > 0)
{
for (int i = 0; i < m_orders.size() && i < mcounterlist.size() ;i++) {
Order o = m_orders.get(i);
TextView c = mcounterlist.get(o.id);
Date dd = null;
try {
dd = sdf.parse(o.endAt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date now = new Date();
long l = (dd.getTime() /1000)- (now.getTime() /1000);
String sss;
if (l < 0)
{
sss = "ENDED";
}
else
{
sss = timeCalculate(l);
}
if(l < 60 * 60 * 24)
{
c.setBackgroundColor(Color.parseColor("#E3170D"));
}
else
{
c.setBackgroundColor(Color.parseColor("#00000000"));
}
c.setText("Time Left: " + sss);
}
}
mHandler.postDelayed(this, 1000);
}
};
But when I scroll through my list, the textviews jump all over the place. I believe part of my issue is related to how I sort the list every 30 seconds, but the ID's of the items don't change. Is there anything obvious that is wrong?
Thanks.
So the issue is that the views in a listview get reused, the solution was to implement the following change the id to the position in my hashmap and put it in GETVIEW
mcounterlist.remove(position);
mcounterlist.put(position , ea);
You probably don't need to remove the position, but I left it in there since I spent a hour on this, and it works so I don't wnat to change anything:)
Then in my repeat task I need to exclude anyitem not visibe, so here is my handler code
int fp = lv.getFirstVisiblePosition() -1 < 0 ? 0 :lv.getFirstVisiblePosition() -1;
int lp = lv.getLastVisiblePosition();
for (int i = fp; i < mcounterlist.size() && i < lp;i++) {
//Log.i("TEST231231",String.valueOf(i));
if (i < lv.getFirstVisiblePosition() -1 )
{
continue;
}
if (i >= lv.getLastVisiblePosition())
{
continue;
}
//my adapter list
Order o = m_orders.get(i);
//since I set the position as the refereance, I can use that for the hashmap
TextView c = mcounterlist.get(i);
...
So in essance since the same textviews were getting recycled, I could not update all of them because items that were not on the list were sharing the active text boxes. To eliminate this issue, I check and only edit the textboxes for items that are visible.

Categories

Resources