· 8 years ago · Mar 14, 2018, 04:22 AM
1
2Save New Duplicate & Edit Just Text Twitter
3>mainAcivity
4
5package com.rsc.annaparrish.todolist;
6
7import android.app.Activity;
8import android.os.Bundle;
9import android.view.View;
10import android.widget.EditText;
11import android.widget.TextView;
12import android.widget.Toast;
13
14public class MainActivity extends Activity {
15
16 EditText userInput;
17 TextView recordsTextView;
18 MyDBHandler dbHandler;
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25 userInput = (EditText) findViewById(R.id.user_Input);
26 recordsTextView = (TextView) findViewById(R.id.records_TextView);
27 dbHandler = new MyDBHandler(this, null, null, 1);
28 printDatabase();
29 }
30
31 public void printDatabase() {
32 String dbString = dbHandler.databaseToString();
33 recordsTextView.setText(dbString);
34 userInput.setText("");
35
36 }
37
38
39 public void addButtonClicked(View view){
40 Products product = new Products(userInput.getText().toString());
41 dbHandler.addProduct(product);
42 printDatabase();
43 }
44
45 public void deleteButtonClicked(View view){
46 String inputText = userInput.getText().toString();
47 dbHandler.deleteProduct(inputText);
48 printDatabase();
49 }
50
51 public void dropButtonClicked(View view) {
52 }
53}
54
55
56_____________________________
57myDBHandler
58
59import android.database.sqlite.SQLiteDatabase;
60import android.database.sqlite.SQLiteOpenHelper;
61import android.database.Cursor;
62import android.content.Context;
63import android.content.ContentValues;
64import android.util.Log;
65
66public class MyDBHandler extends SQLiteOpenHelper {
67 private static final int DATABASE_VERSION = 1;
68 private static final String DATABASE_NAME = "productDB.db";
69 public static final String TABLE_PRODUCTS = "products";
70 public static final String COLUMN_ID = "_id";
71 public static final String COLUMN_PRODUCTNAME = "productname";
72
73 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
74 super(context, DATABASE_NAME, factory, DATABASE_VERSION);
75 }
76
77 @Override
78 public void onCreate(SQLiteDatabase db) {
79 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
80 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
81 COLUMN_PRODUCTNAME + " TEXT " +
82 ");";
83 db.execSQL(query);
84 }
85
86 @Override
87 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
88 db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
89 onCreate(db);
90 }
91
92 public void addProduct(Products product) {
93 ContentValues values = new ContentValues();
94 values.put(COLUMN_PRODUCTNAME, product.get_productname());
95 SQLiteDatabase db = getWritableDatabase();
96 db.insert(TABLE_PRODUCTS, null, values);
97 db.close();
98 }
99
100
101 public int deleteProduct(String productName) {
102 SQLiteDatabase db = getWritableDatabase();
103
104 String whereClause = COLUMN_PRODUCTNAME + " = \"" + productName + "\"";
105 int deleteResult;
106 try {
107 deleteResult = db.delete(TABLE_PRODUCTS, whereClause, null);
108 }
109 catch(Exception e) {
110 Log.e("TODOLIST","delete problem");
111 deleteResult = 0;
112 }
113 return deleteResult;
114 }
115
116
117
118
119
120
121
122
123
124
125 public String databaseToString(){
126 String dbString = "";
127 SQLiteDatabase db = getWritableDatabase();
128 String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";// why not leave out the WHERE clause?
129
130 Cursor recordSet = db.rawQuery(query, null);
131 recordSet.moveToFirst();
132
133 while (!recordSet.isAfterLast()) {
134 if (recordSet.getString(recordSet.getColumnIndex("productname")) != null) {
135 dbString += recordSet.getString(recordSet.getColumnIndex("productname"));
136 dbString += "\n";
137 }
138 recordSet.moveToNext();
139 }
140 db.close();
141 return dbString;
142 }
143
144}
145
146________________________
147
148products
149
150public class Products {
151 private int _id;
152 private String _productname;
153
154 public Products(){
155
156 }
157 public Products(String productName) {
158 this._productname = productName;
159 }
160
161 public int get_id() {
162 return _id;
163 }
164
165 public void set_id(int _id) {
166 this._id = _id;
167 }
168
169 public String get_productname() {
170 return _productname;
171 }
172
173 public void set_productname(String _productname) {
174 this._productname = _productname;
175 }
176}
177
178___________________
179xml
180
181<?xml version="1.0" encoding="utf-8"?>
182
183<RelativeLayout
184 xmlns:android="http://schemas.android.com/apk/res/android"
185 xmlns:app="http://schemas.android.com/apk/res-auto"
186 xmlns:tools="http://schemas.android.com/tools"
187 android:layout_width="match_parent"
188 android:layout_height="match_parent"
189 tools:context="com.rsc.annaparrish.todolist.MainActivity">
190
191
192
193
194 <EditText
195 android:id="@+id/user_Input"
196 android:layout_width="300dp"
197 android:layout_height="wrap_content"
198 android:layout_centerVertical="true"
199 android:layout_below="@+id/scrollView2"
200 android:layout_centerHorizontal="true"
201 android:layout_marginTop="40dp"
202 android:hint="To Do Item Description"
203 android:selectAllOnFocus="false"
204 android:singleLine="false"
205 tools:ignore="MissingPrefix" />
206
207 <EditText
208 android:id="@+id/priorityText"
209 style="@android:style/Widget.Material.Light.EditText"
210 android:layout_width="100dp"
211 android:layout_height="wrap_content"
212 android:layout_below="@+id/user_Input"
213 android:layout_centerHorizontal="true"
214 android:layout_marginTop="22dp"
215 android:hint="Priority 1, 2, 3 (Default is 1" tools:targetApi="lollipop" />
216
217
218 <Button
219 android:id="@+id/add_Button"
220 android:layout_width="wrap_content"
221 android:layout_height="wrap_content"
222 android:layout_alignStart="@+id/user_Input"
223 android:layout_below="@+id/priorityText"
224 android:layout_marginTop="30dp"
225 android:onClick="addButtonClicked"
226 android:text="ADD" />
227
228 <Button
229 android:id="@+id/delete_Button"
230 android:layout_width="wrap_content"
231 android:layout_height="wrap_content"
232 android:layout_alignBaseline="@+id/add_Button"
233 android:layout_alignBottom="@+id/add_Button"
234 android:layout_centerHorizontal="true"
235 android:onClick="deleteButtonClicked"
236 android:text="Delete" />
237
238 <Button
239 android:id="@+id/drop_Button"
240 android:layout_width="wrap_content"
241 android:layout_height="wrap_content"
242 android:layout_alignBaseline="@+id/delete_Button"
243 android:layout_alignBottom="@+id/delete_Button"
244 android:layout_alignEnd="@+id/records_TextView"
245 android:onClick="dropButtonClicked"
246 android:text="DROP!" />
247
248 <ScrollView
249 android:layout_width="wrap_content"
250 android:layout_height="wrap_content"
251 android:id="@+id/scrollView2">
252
253 </ScrollView>
254
255 <TextView
256 android:id="@+id/records_TextView"
257 android:layout_width="300dp"
258 android:layout_height="100dp"
259 android:layout_alignParentBottom="true"
260 android:layout_alignStart="@+id/add_Button"
261 android:layout_marginBottom="20dp" />
262
263
264</RelativeLayout>
265
266mainAcivity
267
268package com.rsc.annaparrish.todolist;
269
270import android.app.Activity;
271import android.os.Bundle;
272import android.view.View;
273import android.widget.EditText;
274import android.widget.TextView;
275import android.widget.Toast;
276
277public class MainActivity extends Activity {
278
279 EditText userInput;
280 TextView recordsTextView;
281 MyDBHandler dbHandler;
282
283 @Override
284 protected void onCreate(Bundle savedInstanceState) {
285 super.onCreate(savedInstanceState);
286 setContentView(R.layout.activity_main);
287
288 userInput = (EditText) findViewById(R.id.user_Input);
289 recordsTextView = (TextView) findViewById(R.id.records_TextView);
290 dbHandler = new MyDBHandler(this, null, null, 1);
291 printDatabase();
292 }
293
294 public void printDatabase() {
295 String dbString = dbHandler.databaseToString();
296 recordsTextView.setText(dbString);
297 userInput.setText("");
298
299 }
300
301
302 public void addButtonClicked(View view){
303 Products product = new Products(userInput.getText().toString());
304 dbHandler.addProduct(product);
305 printDatabase();
306 }
307
308 public void deleteButtonClicked(View view){
309 String inputText = userInput.getText().toString();
310 dbHandler.deleteProduct(inputText);
311 printDatabase();
312 }
313
314 public void dropButtonClicked(View view) {
315 }
316}
317
318
319_____________________________
320myDBHandler
321
322import android.database.sqlite.SQLiteDatabase;
323import android.database.sqlite.SQLiteOpenHelper;
324import android.database.Cursor;
325import android.content.Context;
326import android.content.ContentValues;
327import android.util.Log;
328
329public class MyDBHandler extends SQLiteOpenHelper {
330 private static final int DATABASE_VERSION = 1;
331 private static final String DATABASE_NAME = "productDB.db";
332 public static final String TABLE_PRODUCTS = "products";
333 public static final String COLUMN_ID = "_id";
334 public static final String COLUMN_PRODUCTNAME = "productname";
335
336 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
337 super(context, DATABASE_NAME, factory, DATABASE_VERSION);
338 }
339
340 @Override
341 public void onCreate(SQLiteDatabase db) {
342 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
343 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
344 COLUMN_PRODUCTNAME + " TEXT " +
345 ");";
346 db.execSQL(query);
347 }
348
349 @Override
350 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
351 db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
352 onCreate(db);
353 }
354
355 public void addProduct(Products product) {
356 ContentValues values = new ContentValues();
357 values.put(COLUMN_PRODUCTNAME, product.get_productname());
358 SQLiteDatabase db = getWritableDatabase();
359 db.insert(TABLE_PRODUCTS, null, values);
360 db.close();
361 }
362
363
364 public int deleteProduct(String productName) {
365 SQLiteDatabase db = getWritableDatabase();
366
367 String whereClause = COLUMN_PRODUCTNAME + " = \"" + productName + "\"";
368 int deleteResult;
369 try {
370 deleteResult = db.delete(TABLE_PRODUCTS, whereClause, null);
371 }
372 catch(Exception e) {
373 Log.e("TODOLIST","delete problem");
374 deleteResult = 0;
375 }
376 return deleteResult;
377 }
378
379
380
381
382
383
384
385
386
387
388 public String databaseToString(){
389 String dbString = "";
390 SQLiteDatabase db = getWritableDatabase();
391 String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";// why not leave out the WHERE clause?
392
393 Cursor recordSet = db.rawQuery(query, null);
394 recordSet.moveToFirst();
395
396 while (!recordSet.isAfterLast()) {
397 if (recordSet.getString(recordSet.getColumnIndex("productname")) != null) {
398 dbString += recordSet.getString(recordSet.getColumnIndex("productname"));
399 dbString += "\n";
400 }
401 recordSet.moveToNext();
402 }
403 db.close();
404 return dbString;
405 }
406
407}
408
409________________________
410
411products
412
413public class Products {
414 private int _id;
415 private String _productname;
416
417 public Products(){
418
419 }
420 public Products(String productName) {
421 this._productname = productName;
422 }
423
424 public int get_id() {
425 return _id;
426 }
427
428 public void set_id(int _id) {
429 this._id = _id;
430 }
431
432 public String get_productname() {
433 return _productname;
434 }
435
436 public void set_productname(String _productname) {
437 this._productname = _productname;
438 }
439}
440
441___________________
442xml
443
444<?xml version="1.0" encoding="utf-8"?>
445
446<RelativeLayout
447 xmlns:android="http://schemas.android.com/apk/res/android"
448 xmlns:app="http://schemas.android.com/apk/res-auto"
449 xmlns:tools="http://schemas.android.com/tools"
450 android:layout_width="match_parent"
451 android:layout_height="match_parent"
452 tools:context="com.rsc.annaparrish.todolist.MainActivity">
453
454
455
456
457 <EditText
458 android:id="@+id/user_Input"
459 android:layout_width="300dp"
460 android:layout_height="wrap_content"
461 android:layout_centerVertical="true"
462 android:layout_below="@+id/scrollView2"
463 android:layout_centerHorizontal="true"
464 android:layout_marginTop="40dp"
465 android:hint="To Do Item Description"
466 android:selectAllOnFocus="false"
467 android:singleLine="false"
468 tools:ignore="MissingPrefix" />
469
470 <EditText
471 android:id="@+id/priorityText"
472 style="@android:style/Widget.Material.Light.EditText"
473 android:layout_width="100dp"
474 android:layout_height="wrap_content"
475 android:layout_below="@+id/user_Input"
476 android:layout_centerHorizontal="true"
477 android:layout_marginTop="22dp"
478 android:hint="Priority 1, 2, 3 (Default is 1" tools:targetApi="lollipop" />
479
480
481 <Button
482 android:id="@+id/add_Button"
483 android:layout_width="wrap_content"
484 android:layout_height="wrap_content"
485 android:layout_alignStart="@+id/user_Input"
486 android:layout_below="@+id/priorityText"
487 android:layout_marginTop="30dp"
488 android:onClick="addButtonClicked"
489 android:text="ADD" />
490
491 <Button
492 android:id="@+id/delete_Button"
493 android:layout_width="wrap_content"
494 android:layout_height="wrap_content"
495 android:layout_alignBaseline="@+id/add_Button"
496 android:layout_alignBottom="@+id/add_Button"
497 android:layout_centerHorizontal="true"
498 android:onClick="deleteButtonClicked"
499 android:text="Delete" />
500
501 <Button
502 android:id="@+id/drop_Button"
503 android:layout_width="wrap_content"
504 android:layout_height="wrap_content"
505 android:layout_alignBaseline="@+id/delete_Button"
506 android:layout_alignBottom="@+id/delete_Button"
507 android:layout_alignEnd="@+id/records_TextView"
508 android:onClick="dropButtonClicked"
509 android:text="DROP!" />
510
511 <ScrollView
512 android:layout_width="wrap_content"
513 android:layout_height="wrap_content"
514 android:id="@+id/scrollView2">
515
516 </ScrollView>
517
518 <TextView
519 android:id="@+id/records_TextView"
520 android:layout_width="300dp"
521 android:layout_height="100dp"
522 android:layout_alignParentBottom="true"
523 android:layout_alignStart="@+id/add_Button"
524 android:layout_marginBottom="20dp" />
525
526
527</RelativeLayout>