|
13 | 13 | import android.widget.Button; |
14 | 14 | import android.widget.EditText; |
15 | 15 | import de.mvhs.android.zeiterfassung.db.DBHelper; |
| 16 | +import de.mvhs.android.zeiterfassung.db.ZeitContracts; |
16 | 17 |
|
17 | 18 | public class MainActivity extends Activity { |
18 | 19 |
|
19 | | - private boolean _IsStarted = false; |
20 | | - |
21 | | - @Override |
22 | | - protected void onCreate(Bundle savedInstanceState) { |
23 | | - super.onCreate(savedInstanceState); |
24 | | - |
25 | | - setContentView(R.layout.activity_main); |
26 | | - |
27 | | - if (savedInstanceState != null) { |
28 | | - _IsStarted = savedInstanceState.getBoolean("CurrentState", false); |
29 | | - } |
30 | | - |
31 | | - setButtonState(); |
32 | | - } |
33 | | - |
34 | | - @Override |
35 | | - protected void onStart() { |
36 | | - super.onStart(); |
37 | | - |
38 | | - // Buttons registrieren |
39 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
40 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
41 | | - |
42 | | - // Click Event registrieren |
43 | | - commandStart.setOnClickListener(new OnStartButtonClicked()); |
44 | | - |
45 | | - commandEnd.setOnClickListener(new OnEndButtonClicked()); |
46 | | - } |
47 | | - |
48 | | - @Override |
49 | | - protected void onSaveInstanceState(Bundle outState) { |
50 | | - outState.putBoolean("CurrentState", _IsStarted); |
51 | | - super.onSaveInstanceState(outState); |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - protected void onStop() { |
56 | | - // Buttons registrieren |
57 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
58 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
59 | | - |
60 | | - // Click Event deregistrieren |
61 | | - commandStart.setOnClickListener(null); |
62 | | - commandEnd.setOnClickListener(null); |
63 | | - super.onStop(); |
64 | | - } |
65 | | - |
66 | | - private void setButtonState() { |
67 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
68 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
69 | | - |
70 | | - commandStart.setEnabled(_IsStarted == false); |
71 | | - commandEnd.setEnabled(_IsStarted); |
72 | | - } |
73 | | - |
74 | | - private final class OnEndButtonClicked implements OnClickListener { |
75 | | - @Override |
76 | | - public void onClick(View v) { |
77 | | - // Verhalten beim Click auf den Ende-Button |
78 | | - EditText endTime = (EditText) findViewById(R.id.EndTime); |
79 | | - endTime.setText(new Date().toString()); |
80 | | - _IsStarted = false; |
81 | | - setButtonState(); |
82 | | - |
83 | | - DBHelper helper = new DBHelper(MainActivity.this); |
84 | | - SQLiteDatabase db = helper.getReadableDatabase(); |
85 | | - Cursor data = db.query("zeit", // Tabelle |
86 | | - new String[] { BaseColumns._ID }, // Spalten |
87 | | - "IFNULL(end_time,'')=''", // Bedingung |
88 | | - null, // Argumente für die bedingung |
89 | | - null, // Grupierung |
90 | | - null, // Having |
91 | | - null); // Sortierung |
92 | | - if (data != null && data.moveToFirst()) { |
93 | | - // Datensatz gefunden, kann aktualisiert werden |
94 | | - SQLiteDatabase updateDb = helper.getWritableDatabase(); |
95 | | - long id = data.getLong(0); |
96 | | - |
97 | | - ContentValues values = new ContentValues(); |
98 | | - values.put("end_time", new Date().toString()); |
99 | | - |
100 | | - updateDb.update("zeit", values, "_id=?", new String[] { String.valueOf(id) }); |
101 | | - |
102 | | - updateDb.close(); |
103 | | - } |
104 | | - |
105 | | - // Alles schließen |
106 | | - if (data != null) { |
107 | | - data.close(); |
108 | | - } |
109 | | - db.close(); |
110 | | - helper.close(); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - private final class OnStartButtonClicked implements OnClickListener { |
115 | | - @Override |
116 | | - public void onClick(View v) { |
117 | | - // Verhalten beim Klick auf den Strat Button |
118 | | - EditText startTime = (EditText) findViewById(R.id.StartTime); |
119 | | - startTime.setText(new Date().toString()); |
120 | | - _IsStarted = true; |
121 | | - setButtonState(); |
122 | | - |
123 | | - DBHelper helper = new DBHelper(MainActivity.this); |
124 | | - SQLiteDatabase db = helper.getWritableDatabase(); |
125 | | - |
126 | | - ContentValues values = new ContentValues(); |
127 | | - values.put("start_time", new Date().toString()); |
128 | | - |
129 | | - db.insert("zeit", null, values); |
130 | | - |
131 | | - db.close(); |
132 | | - } |
133 | | - } |
| 20 | + private boolean _IsStarted = false; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + |
| 26 | + setContentView(R.layout.activity_main); |
| 27 | + |
| 28 | + if (savedInstanceState != null) { |
| 29 | + _IsStarted = savedInstanceState.getBoolean("CurrentState", false); |
| 30 | + } |
| 31 | + |
| 32 | + setButtonState(); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onStart() { |
| 37 | + super.onStart(); |
| 38 | + |
| 39 | + // Buttons registrieren |
| 40 | + Button commandStart = (Button) findViewById(R.id.StartCommand); |
| 41 | + Button commandEnd = (Button) findViewById(R.id.EndCommand); |
| 42 | + |
| 43 | + // Click Event registrieren |
| 44 | + commandStart.setOnClickListener(new OnStartButtonClicked()); |
| 45 | + |
| 46 | + commandEnd.setOnClickListener(new OnEndButtonClicked()); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void onSaveInstanceState(Bundle outState) { |
| 51 | + outState.putBoolean("CurrentState", _IsStarted); |
| 52 | + super.onSaveInstanceState(outState); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void onStop() { |
| 57 | + // Buttons registrieren |
| 58 | + Button commandStart = (Button) findViewById(R.id.StartCommand); |
| 59 | + Button commandEnd = (Button) findViewById(R.id.EndCommand); |
| 60 | + |
| 61 | + // Click Event deregistrieren |
| 62 | + commandStart.setOnClickListener(null); |
| 63 | + commandEnd.setOnClickListener(null); |
| 64 | + super.onStop(); |
| 65 | + } |
| 66 | + |
| 67 | + private void setButtonState() { |
| 68 | + Button commandStart = (Button) findViewById(R.id.StartCommand); |
| 69 | + Button commandEnd = (Button) findViewById(R.id.EndCommand); |
| 70 | + |
| 71 | + commandStart.setEnabled(_IsStarted == false); |
| 72 | + commandEnd.setEnabled(_IsStarted); |
| 73 | + } |
| 74 | + |
| 75 | + private final class OnEndButtonClicked implements OnClickListener { |
| 76 | + @Override |
| 77 | + public void onClick(View v) { |
| 78 | + // Verhalten beim Click auf den Ende-Button |
| 79 | + EditText endTime = (EditText) findViewById(R.id.EndTime); |
| 80 | + endTime.setText(new Date().toString()); |
| 81 | + _IsStarted = false; |
| 82 | + setButtonState(); |
| 83 | + |
| 84 | + DBHelper helper = new DBHelper(MainActivity.this); |
| 85 | + SQLiteDatabase db = helper.getReadableDatabase(); |
| 86 | + Cursor data = db.query("zeit", // Tabelle |
| 87 | + new String[] { BaseColumns._ID }, // Spalten |
| 88 | + "IFNULL(end_time,'')=''", // Bedingung |
| 89 | + null, // Argumente für die bedingung |
| 90 | + null, // Grupierung |
| 91 | + null, // Having |
| 92 | + null); // Sortierung |
| 93 | + if (data != null && data.moveToFirst()) { |
| 94 | + // Datensatz gefunden, kann aktualisiert werden |
| 95 | + SQLiteDatabase updateDb = helper.getWritableDatabase(); |
| 96 | + long id = data.getLong(0); |
| 97 | + |
| 98 | + ContentValues values = new ContentValues(); |
| 99 | + values.put("end_time", new Date().toString()); |
| 100 | + |
| 101 | + updateDb.update("zeit", values, "_id=?", |
| 102 | + new String[] { String.valueOf(id) }); |
| 103 | + |
| 104 | + updateDb.close(); |
| 105 | + } |
| 106 | + |
| 107 | + // Alles schließen |
| 108 | + if (data != null) { |
| 109 | + data.close(); |
| 110 | + } |
| 111 | + db.close(); |
| 112 | + helper.close(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private final class OnStartButtonClicked implements OnClickListener { |
| 117 | + @Override |
| 118 | + public void onClick(View v) { |
| 119 | + // Verhalten beim Klick auf den Strat Button |
| 120 | + EditText startTime = (EditText) findViewById(R.id.StartTime); |
| 121 | + startTime.setText(new Date().toString()); |
| 122 | + _IsStarted = true; |
| 123 | + setButtonState(); |
| 124 | + |
| 125 | + // DBHelper helper = new DBHelper(MainActivity.this); |
| 126 | + // SQLiteDatabase db = helper.getWritableDatabase(); |
| 127 | + // |
| 128 | + // ContentValues values = new ContentValues(); |
| 129 | + // values.put("start_time", new Date().toString()); |
| 130 | + // |
| 131 | + // db.insert("zeit", null, values); |
| 132 | + // |
| 133 | + // db.close(); |
| 134 | + |
| 135 | + ContentValues values = new ContentValues(); |
| 136 | + values.put(ZeitContracts.Zeit.Columns.START, new Date().toString()); |
| 137 | + |
| 138 | + getContentResolver().insert(ZeitContracts.Zeit.CONTENT_URI, values); |
| 139 | + } |
| 140 | + |
| 141 | + } |
134 | 142 | } |
0 commit comments