· 8 years ago · Mar 15, 2018, 07:16 PM
1package com.example.root.myapplication;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.util.Log;
9
10import java.util.ArrayList;
11import java.util.Collections;
12
13public class DBPlanner {
14
15 private SQLiteDatabase mDataBase;
16
17 public DBPlanner(Context context) {
18 OpenHelper mOpenHelper = new OpenHelper(context);
19 mDataBase = mOpenHelper.getWritableDatabase();
20 }
21
22 public void delete(String Plan) {
23 mDataBase.delete("PLANNER", "PLAN = ?", new String[]{String.valueOf(Plan)});
24 }
25
26 public void deleteAll() {
27 mDataBase.delete("PLANNER", null, null);
28 }
29
30 public void insert(Plans plans) {
31 ContentValues cv = new ContentValues();
32 cv.put("DAYS", plans.getDay());
33 cv.put("MONTHS", plans.getMonth());
34 cv.put("YEARS", plans.getYear());
35 cv.put("HOURS", plans.getHours());
36 cv.put("MINUTES", plans.getMinutes());
37 cv.put("PLAN", plans.getPlan());
38 cv.put("IMPORTANCE", plans.getImportance());
39 cv.put("URGENCY", plans.getUrgency());
40 mDataBase.insert("PLANNER", null, cv);
41 }
42
43 public ArrayList<Plans> selectAll() {
44 Cursor mCursor = mDataBase.query("PLANNER", null, null, null, null, null, null);
45 ArrayList<Plans> plans = new ArrayList<Plans>();
46 mCursor.moveToFirst();
47 while (!mCursor.isAfterLast()) {
48 int d = mCursor.getInt(0);
49 int m = mCursor.getInt(1);
50 int y = mCursor.getInt(2);
51 int hours = mCursor.getInt(3);
52 int minutes = mCursor.getInt(4);
53 String plan = mCursor.getString(5);
54 String importance = mCursor.getString(6);
55 String urgency = mCursor.getString(7);
56 plans.add(new Plans(d, m, y, hours, minutes, plan, importance, urgency));
57 mCursor.moveToNext();
58 }
59 return plans;
60 }
61
62 public ArrayList<Plans> selectCertain(long day, long month, long year) {
63 Cursor mCursor = mDataBase.query("PLANNER", null, "YEARS = ? AND MONTHS = ? AND DAYS = ?", new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(day)}, null, null, null, null);
64 ArrayList<Plans> plans = new ArrayList<Plans>();
65 mCursor.moveToFirst();
66 while (!mCursor.isAfterLast()) {
67 int d = mCursor.getInt(0);
68 int m = mCursor.getInt(1);
69 int y = mCursor.getInt(2);
70 int hours = mCursor.getInt(3);
71 int minutes = mCursor.getInt(4);
72 String plan = mCursor.getString(5);
73 String importance = mCursor.getString(6);
74 String urgency = mCursor.getString(7);
75 if (d == day && m == month && y == year)
76 plans.add(new Plans(d, m, y, hours, minutes, plan, importance, urgency));
77 mCursor.moveToNext();
78 }
79 Collections.sort(plans);
80 return plans;
81 }
82
83 public int compare(Plans o, Plans t1) {
84 long a = o.getMinutes() + o.getHours() * 60;
85 long b = t1.getMinutes() + t1.getHours() * 60;
86 if (a > b + 1)
87 return -1;
88 if (a < b - 1)
89 return 1;
90 return 0;
91
92 }
93
94 public int update(Plans plans) {
95 ContentValues cv = new ContentValues();
96 cv.put("DAYS", plans.getDay());
97 cv.put("MONTHS", plans.getMonth());
98 cv.put("YEARS", plans.getYear());
99 cv.put("HOURS", plans.getHours());
100 cv.put("MINUTES", plans.getMinutes());
101 cv.put("PLAN", plans.getPlan());
102 cv.put("IMPORTANCE", plans.getImportance());
103 cv.put("URGENCY", plans.getUrgency());
104 return mDataBase.update("PLANNER", cv, "PLAN = ?", new String[]{String.valueOf(plans.getPlan())});
105 }
106
107
108 private class OpenHelper extends SQLiteOpenHelper {
109
110 OpenHelper(Context context) {
111 super(context, "PLANNERDB", null, 1);
112 }
113
114 @Override
115 public void onCreate(SQLiteDatabase db) {
116 String query = "CREATE TABLE IF NOT EXISTS PLANNER(\n" +
117 " DAYS,\n" +
118 " MONTHS,\n" +
119 " YEARS,\n" +
120 " HOURS,\n" +
121 " MINUTES,\n" +
122 " PLAN NOT null,\n" +
123 " IMPORTANCE NOT null,\n" +
124 " URGENCY NOT null\n" +
125 " )";
126 db.execSQL(query);
127 }
128
129 @Override
130 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
131 }
132 }
133}
134
135
136
137
138package com.example.root.myapplication;
139
140import android.content.Intent;
141import android.support.v7.app.AppCompatActivity;
142import android.os.Bundle;
143import android.util.Log;
144import android.view.View;
145import android.widget.Button;
146import android.widget.ListView;
147
148import java.text.DateFormat;
149import java.text.SimpleDateFormat;
150import java.util.ArrayList;
151import java.util.Calendar;
152import java.util.Date;
153import java.util.GregorianCalendar;
154
155public class PlannerActivity extends AppCompatActivity implements View.OnClickListener {
156 Date date = new Date();
157 Calendar calendar;
158 ListView listView;
159 ArrayList<Plans> plan = new ArrayList<Plans>();
160 DBPlanner dbPlanner;
161
162 @Override
163 protected void onCreate(Bundle savedInstanceState) {
164 super.onCreate(savedInstanceState);
165 setContentView(R.layout.activity_planner);
166 dbPlanner = new DBPlanner(this);
167 getSupportActionBar().hide();
168 Calendar calendar = Calendar.getInstance();
169 calendar.setTime(date);
170 SimpleDateFormat yearForm = new SimpleDateFormat("yyyy");
171 SimpleDateFormat monthForm = new SimpleDateFormat("MM");
172 SimpleDateFormat dayForm = new SimpleDateFormat("dd");
173 int year = 0, month = 0, day = 0;
174 year = Integer.parseInt(yearForm.format(calendar.getTime()));
175 month = Integer.parseInt(monthForm.format(calendar.getTime()));
176 day = Integer.parseInt(dayForm.format(calendar.getTime()));
177 Button buttonAdd = findViewById(R.id.btnAdd);
178 buttonAdd.setOnClickListener(this);
179 plan = dbPlanner.selectCertain(day,month,year);
180 listView = findViewById(R.id.listView);
181 PlannerAdapter planAdapter = new PlannerAdapter(this, plan);
182 listView.setAdapter(planAdapter);
183 }
184
185 @Override
186 public void onClick(View view) {
187
188 switch (view.getId()) {
189 case R.id.btnAdd:
190 Intent intent = new Intent(getApplicationContext(), AddActivity.class);
191 startActivityForResult(intent, 1);
192 break;
193 }
194
195 }
196}
197
198
199
200
201
202
203
204
205
206
207
208
209package com.example.root.myapplication;
210
211import android.content.Intent;
212import android.support.v7.app.AppCompatActivity;
213import android.os.Bundle;
214import android.util.Log;
215import android.view.View;
216import android.widget.Button;
217import android.widget.EditText;
218
219public class AddActivity extends AppCompatActivity implements View.OnClickListener {
220 DBPlanner dbPlanner;
221 EditText edtYear, edtMonth, edtDay, edtHour, edtMinutes, edtPlan, edtUrg, edtImp;
222
223 @Override
224 protected void onCreate(Bundle savedInstanceState) {
225 super.onCreate(savedInstanceState);
226 setContentView(R.layout.activity_add);
227 dbPlanner = new DBPlanner(this);
228 getSupportActionBar().hide();
229 Button btnCreate = findViewById(R.id.btnCreate);
230 btnCreate.setOnClickListener(this);
231 }
232
233 public void getEdit() {
234 edtYear = findViewById(R.id.year);
235 edtMonth = findViewById(R.id.month);
236 edtDay = findViewById(R.id.day);
237 edtHour = findViewById(R.id.hours);
238 edtMinutes = findViewById(R.id.minutes);
239 edtPlan = findViewById(R.id.plan);
240 edtUrg = findViewById(R.id.urg);
241 edtImp = findViewById(R.id.imp);
242 }
243
244 @Override
245 public void onClick(View view) {
246 switch (view.getId()) {
247 case R.id.btnCreate:
248 Integer day = 0, month = 0, year = 0, hours = 0, minutes = 0;
249 String plan = "", imp = "", urg = "";
250 getEdit();
251 day = Integer.parseInt(String.valueOf(edtDay.getText()));
252 month = Integer.parseInt(String.valueOf(edtMonth.getText()));
253 year = Integer.parseInt(String.valueOf(edtYear.getText()));
254 hours = Integer.parseInt(String.valueOf(edtHour.getText()));
255 minutes = Integer.parseInt(String.valueOf(edtMinutes.getText()));
256 plan = String.valueOf(edtPlan.getText());
257 imp = String.valueOf(edtImp.getText());
258 urg = String.valueOf(edtUrg.getText());
259 Plans plans = new Plans(day, month, year, hours, minutes, plan, imp, urg);
260 dbPlanner.insert(plans);
261 Intent intent = new Intent(getApplicationContext(), PlannerActivity.class);
262 startActivityForResult(intent, 0);
263 break;
264 }
265 }
266}