· 8 years ago · Jul 12, 2018, 12:18 AM
1## Homework.java
2package com.android.homework;
3
4import java.util.Calendar;
5
6import android.app.Activity;
7import android.app.AlertDialog;
8import android.app.DatePickerDialog;
9import android.app.Dialog;
10import android.content.DialogInterface;
11import android.os.Bundle;
12import android.view.View;
13import android.widget.AdapterView;
14import android.widget.ArrayAdapter;
15import android.widget.Button;
16import android.widget.DatePicker;
17import android.widget.EditText;
18import android.widget.Spinner;
19import android.widget.TextView;
20
21
22public class Homework extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
23 HomeworkDB db = new HomeworkDB( this );
24 EditText title;
25 Spinner pick;
26 Button btnSave;
27 Button btnCancel;
28 // DATE
29 private TextView mDateDisplay;
30 private Button mPickDate;
31
32 private int mYear;
33 private int mMonth;
34 private int mDay;
35
36 static final int DATE_DIALOG_ID = 0;
37
38 // the callback received when the user "sets" the date in the dialog
39 private DatePickerDialog.OnDateSetListener mDateSetListener =
40 new DatePickerDialog.OnDateSetListener() {
41
42 public void onDateSet(DatePicker view, int year,
43 int monthOfYear, int dayOfMonth) {
44 mYear = year;
45 mMonth = monthOfYear;
46 mDay = dayOfMonth;
47 updateDisplay();
48 }
49 };
50
51
52 @Override
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.main);
56
57 title = (EditText) findViewById(R.id.EditText01);
58 pick = (Spinner) findViewById(R.id.Spinner01);
59 btnSave = (Button) findViewById(R.id.Button01);
60 btnCancel = (Button) findViewById(R.id.Button02);
61
62 title.setText("Task name");
63 pick.setOnItemSelectedListener(this);
64 btnSave.setText("Save");
65 btnCancel.setText("Cancel");
66
67 ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.subjects, android.R.layout.simple_spinner_item );
68 adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
69 pick.setAdapter(adapter);
70
71 btnSave.setOnClickListener(this);
72 btnCancel.setOnClickListener(this);
73
74 //############## DATE ##############
75 // capture our View elements
76 mDateDisplay = (TextView) findViewById(R.id.TextView01);
77 mPickDate = (Button) findViewById(R.id.Button03);
78 mPickDate.setText("Set date");
79
80 // add a click listener to the button
81 mPickDate.setOnClickListener(new View.OnClickListener() {
82 public void onClick(View v) {
83 showDialog(DATE_DIALOG_ID);
84 }
85 });
86
87 // get the current date
88 final Calendar c = Calendar.getInstance();
89 mYear = c.get(Calendar.YEAR);
90 mMonth = c.get(Calendar.MONTH);
91 mDay = c.get(Calendar.DAY_OF_MONTH);
92
93 // display the current date
94 updateDisplay();
95 }
96
97 @Override
98 protected Dialog onCreateDialog(int id) {
99 switch (id) {
100 case DATE_DIALOG_ID:
101 return new DatePickerDialog(this,
102 mDateSetListener,
103 mYear, mMonth, mDay);
104 }
105 return null;
106 }
107
108 // updates the date we display in the TextView
109 private void updateDisplay() {
110 mDateDisplay.setText(
111 new StringBuilder()
112 // Month is 0 based so add 1
113 .append(mDay).append("-")
114 .append(mMonth + 1).append("-")
115 .append(mYear).append(" "));
116 }
117
118
119 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
120 //View selected = parent.getChildAt( position );
121 //selected.setBackgroundColor( android.R.color.background_dark );
122 }
123
124 @Override
125 public void onNothingSelected(AdapterView<?> arg0) {
126 }
127
128 private void msg( String msg ) {
129 AlertDialog.Builder builder = new AlertDialog.Builder(this);
130 builder.setMessage(msg)
131 .setCancelable(false)
132 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
133 public void onClick(DialogInterface dialog, int id) {
134 dialog.cancel();
135 }
136 })
137 .setNegativeButton("No", new DialogInterface.OnClickListener() {
138 public void onClick(DialogInterface dialog, int id) {
139 dialog.cancel();
140 }
141 });
142 AlertDialog alert = builder.create();
143 alert.show();
144 }
145
146 @Override
147 public void onClick(View v) {
148 // check which button was pressed
149 if( btnCancel.isPressed() ) {
150 AlertDialog.Builder builder = new AlertDialog.Builder(this);
151 builder.setMessage("Are you sure you want to exit?")
152 .setCancelable(false)
153 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
154 public void onClick(DialogInterface dialog, int id) {
155 Homework.this.finish();
156 }
157 })
158 .setNegativeButton("No", new DialogInterface.OnClickListener() {
159 public void onClick(DialogInterface dialog, int id) {
160 dialog.cancel();
161 }
162 });
163 AlertDialog alert = builder.create();
164 alert.show();
165
166 } else if( btnSave.isPressed() ) { // add task
167
168 // date & time already stored in variables
169 String taskname = title.getText().toString();
170 String subject = pick.getSelectedItem().toString();
171
172 if( db.addTask( taskname, subject, mDay, mMonth, mYear ) ) {
173 AlertDialog.Builder builder = new AlertDialog.Builder(this);
174 builder.setMessage("Saved. YAY!")
175 .setCancelable(false)
176 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
177 public void onClick(DialogInterface dialog, int id) {
178 Homework.this.finish();
179 }
180 })
181 .setNegativeButton("No", new DialogInterface.OnClickListener() {
182 public void onClick(DialogInterface dialog, int id) {
183 dialog.cancel();
184 }
185 });
186 AlertDialog alert = builder.create();
187 alert.show();
188 } else {
189 AlertDialog.Builder builder = new AlertDialog.Builder(this);
190 builder.setMessage("Error!")
191 .setCancelable(false)
192 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
193 public void onClick(DialogInterface dialog, int id) {
194 Homework.this.finish();
195 }
196 })
197 .setNegativeButton("No", new DialogInterface.OnClickListener() {
198 public void onClick(DialogInterface dialog, int id) {
199 dialog.cancel();
200 }
201 });
202 AlertDialog alert = builder.create();
203 alert.show();
204 }
205
206 }
207
208
209 }
210
211}
212
213## HomeworkDB.java
214package com.android.homework;
215
216import java.util.ArrayList;
217import java.util.List;
218
219import android.content.ContentValues;
220import android.content.Context;
221import android.database.Cursor;
222import android.database.SQLException;
223import android.database.sqlite.*;
224import android.util.Log;
225
226public class HomeworkDB extends SQLiteOpenHelper {
227 private static final String DATABASE_NAME = "homework";
228 private static final int DATABASE_VERSION = 1;
229 private static final String TABLE_TASKS = "tasks";
230 private static final String TABLE_SUBJECTS = "subjects";
231 private static final String CREATE_TABLE_TASKS = "create table if not exists tasks (task_id integer PRIMARY KEY autoincrement, name text NOT NULL, subject text NOT NULL, day integer NOT NULL, month integer NOT NULL, year integer NOT NULL);";
232 private static final String CREATE_TABLE_SUBJECTS = "create table if not exists subjects(subject_id integer PRIMARY KEY autoincrement, name text NOT NULL);";
233
234 SQLiteDatabase mydb;
235
236 public HomeworkDB(Context ctx) {
237 super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
238 }
239
240 public boolean addSubject( String subject ) {
241 ContentValues values = new ContentValues();
242 values.put( "subject", subject );
243 return ( mydb.insert( TABLE_SUBJECTS, null, values ) > 0 );
244 }
245
246 public boolean deleteSubject( Long subjectId ) {
247 return ( mydb.delete( TABLE_SUBJECTS, "subject_id=" + subjectId.toString(), null ) > 0 );
248 }
249
250 public boolean addTask( String title, String subject, int day, int month, int year ) {
251 mydb = this.getWritableDatabase();
252 ContentValues values = new ContentValues();
253 values.put( "name", title );
254 values.put( "subject", subject );
255 values.put( "day", day );
256 values.put( "month", month );
257 values.put( "year", year );
258 return ( mydb.insert( TABLE_TASKS, null, values ) > 0 );
259 }
260
261 public boolean deleteTask( Long taskId ) {
262 return ( mydb.delete( TABLE_TASKS, "task_id=" + taskId.toString(), null ) > 0 );
263 }
264
265 public List<Task> getTasks( Long taskId ) {
266 ArrayList<Task> tasks = new ArrayList<Task>();
267
268 try {
269 Cursor c = mydb.query(TABLE_TASKS, new String[] { "task_id", "name", "subject", "day", "month", "year" }, "task_id=" + taskId.toString(), null, null, null, null );
270 int nr = c.getCount();
271 c.moveToFirst();
272 for( int i=0; i<nr; ++i ) {
273 Task task = new Task();
274 task.taskId = c.getLong(0);
275 task.name = c.getString(1);
276 task.subject = c.getString(2);
277 task.day = c.getInt(3);
278 task.month = c.getInt(4);
279 task.year = c.getInt(5);
280
281 tasks.add(task);
282
283 c.moveToNext();
284 }
285 } catch ( SQLException e ) {
286 Log.e("Homework", e.toString() );
287 }
288 return tasks;
289 }
290
291 @Override
292 public void onCreate(SQLiteDatabase db) {
293
294 // create tables
295 db.execSQL( CREATE_TABLE_SUBJECTS );
296 db.execSQL( CREATE_TABLE_TASKS );
297
298 mydb = this.getWritableDatabase();
299
300 // default subjects
301 addSubject("Mathematik");
302
303 }
304
305 @Override
306 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
307 // do nothing
308 }
309
310}