ConzT
Newbie
Hey everyone!
Im really new to Android programming(started yesterday) and im currently working on an App that should give me the time between first time clicking a button and second time clicking the button.
It works fine when i stay in the Activity. But if i change the activity while the time is "running" and then reenter the main activity it gives me a strange timestamp.
How the timestamp works:
I have a button for Start/Stop
On clicking start it calls a Method where i get the current system time in milliseconds and saves it to a variable.
On clicking stop it does the same and subtracts endTime-startTime. Thats how i get the time.
(Works fine)
But when changing activity(I got a button where it changes to an activity where i can add a customer) and reentering main and stopping the timer, it adds the totalTime up to something i cant relate to.. currently my stop time is at 45 minutes.
Maybe i do something wrong on saving my values?
I'll just post my code. Maybe someone can help me and give me a hint. Thanks and sorry for my bad english!
Class "Timerecording"
Class "AddCustomer"
I just realised, maybe its because at the end of method addKunde() I start the MainActivity again?
Im really new to Android programming(started yesterday) and im currently working on an App that should give me the time between first time clicking a button and second time clicking the button.
It works fine when i stay in the Activity. But if i change the activity while the time is "running" and then reenter the main activity it gives me a strange timestamp.
How the timestamp works:
I have a button for Start/Stop
On clicking start it calls a Method where i get the current system time in milliseconds and saves it to a variable.
On clicking stop it does the same and subtracts endTime-startTime. Thats how i get the time.
(Works fine)
But when changing activity(I got a button where it changes to an activity where i can add a customer) and reentering main and stopping the timer, it adds the totalTime up to something i cant relate to.. currently my stop time is at 45 minutes.
Maybe i do something wrong on saving my values?
I'll just post my code. Maybe someone can help me and give me a hint. Thanks and sorry for my bad english!
Class "Timerecording"
Java:
package com.example.cmsolutions.zeiterfassung;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View;
import java.lang.reflect.Array;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
public class ZeitErfassen extends AppCompatActivity {
public static LinkedList<Kunde> kunden = new LinkedList();
boolean running = false;
long startTime,endTime,totalTime;
public Date date = new Date();
private SharedPreferences app_preferences;
private SharedPreferences.Editor editor;
private static final int PREFERENCE_MODE_PRIVAT=0;
private TextView displayTime;
public Button startEndButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zeit_erfassen);
//Einstellungen laden
app_preferences = getPreferences(PREFERENCE_MODE_PRIVAT);
displayTime = (TextView)findViewById(R.id.zeit_bei_Kunde);
startTime= app_preferences.getLong("startTime", 0);
endTime = app_preferences.getLong("endTime", 0);
running = app_preferences.getBoolean("running", false);
totalTime = app_preferences.getLong("totalTime", 0);
displayTime.setText((CharSequence) app_preferences.getString("zeitAnzeige", "Zeit bei Kunde"));
startEndButton = (Button)findViewById(R.id.start_Timer);
startEndButton.setText((CharSequence)app_preferences.getString("timerButton","Start Timer"));
editor = app_preferences.edit();
editor.commit();
createDropDown();
}
public void startTimer(View view) {
if(running == false) {
startTime = getTime();
displayTime.setText("Zeitstoppung läuft");
editor.putString("zeitAnzeige",(String)displayTime.getText());
running = true;
editor.putBoolean("running",true);
editor.putLong("startTimer", startTime);
startEndButton.setText("End Timer");
editor.putString("timerButton", (String)startEndButton.getText());
editor.commit();
} else {
endTime = getTime();
editor.putLong("endTime",endTime);
totalTime = endTime - startTime;
editor.putLong("totalTime",totalTime);
int hours = (int) ((totalTime / (1000*60*60)) % 24);
int minutes = (int) ((totalTime / (1000*60)) % 60);
int seconds = (int) (totalTime / 1000) % 60;
displayTime.setText(String.valueOf(hours)+ ":"+String.valueOf(minutes)+":"+ String.valueOf(seconds));
startEndButton.setText("Start Timer");
editor.putString("timerButton",(String)startEndButton.getText());
editor.commit();
running = false;
}
}
public void neuerKunde(View view) {
Intent intent = new Intent(this, AddKunde.class);
startActivity(intent);
}
public long getTime() {
long millis = System.currentTimeMillis();
return millis;
}
public void createDropDown() {
if(kunden.size() > 0) {
Spinner spinner = (Spinner) findViewById(R.id.chooseCustomer);
ArrayList<String> names = new ArrayList<>();
for(Kunde k:kunden) {
names.add(k.getName());
}
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
}
}
Class "AddCustomer"
Code:
package com.example.cmsolutions.zeiterfassung;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import java.util.LinkedList;
public class AddKunde extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_kunde2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void addKunde(View view) throws Exception {
try {
EditText strings = (EditText) findViewById(R.id.customerName);
String name = strings.getText().toString();
strings = (EditText) findViewById(R.id.addressField);
String address = strings.getText().toString();
Kunde customer = new Kunde(name,address);
ZeitErfassen.kunden.add(customer);
} catch (Exception e) {
throw new Exception("Fehler in addKunde!");
}
startActivity(new Intent(this,ZeitErfassen.class));
}
}
I just realised, maybe its because at the end of method addKunde() I start the MainActivity again?

isplayCustomer) I call my overview for all customers where the ListView is filled with data as described above. At the end ob the List I see the customer i just created,xyz.