· 8 years ago · Apr 23, 2018, 04:36 AM
1Main Activity:
2import android.app.Activity;
3import android.os.Bundle;
4import android.view.View;
5import android.widget.EditText;
6import android.widget.TextView;
7
8public class MainActivity extends Activity {
9// Declare references
10
11 EditText userInput;
12 TextView recordsTextView;
13 MyDBHandler dbHandler;
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 userInput = (EditText) findViewById(R.id.user_Input);
21 recordsTextView = (TextView) findViewById(R.id.records_TextView);
22 /* Can pass nulls because of the constants in the helper.
23 * the 1 means version 1 so don't run update.
24 */
25 dbHandler = new MyDBHandler(this, null, null, 1);
26 printDatabase();
27 }
28
29 //Print the database
30 public void printDatabase(){
31 String dbString = dbHandler.databaseToString();
32 recordsTextView.setText(dbString);
33 userInput.setText("");
34 }
35
36 //add your elements onclick methods.
37 //Add a product to the database
38 public void addButtonClicked(View view){
39 // dbHandler.add needs an object parameter.
40 Products product = new Products(userInput.getText().toString());
41 dbHandler.addProduct(product);
42 printDatabase();
43 }
44
45 //Delete items
46 public void deleteButtonClicked(View view){
47 // dbHandler delete needs string to find in the db
48 String inputText = userInput.getText().toString();
49 dbHandler.deleteProduct(inputText);
50 printDatabase();
51 }
52
53}
54
55import android.database.sqlite.SQLiteDatabase;
56import android.database.sqlite.SQLiteOpenHelper;
57import android.database.Cursor;
58import android.content.Context;
59import android.content.ContentValues;
60
61 public class MyDBHandler extends SQLiteOpenHelper{
62 private static final int DATABASE_VERSION = 1;
63 private static final String DATABASE_NAME = "productDB.db";
64 public static final String TABLE_PRODUCTS = "products";
65 public static final String COLUMN_ID = "_id";
66 public static final String COLUMN_PRODUCTNAME = "productname";
67
68 //We need to pass database information along to superclass
69 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
70 super(context, DATABASE_NAME, factory, DATABASE_VERSION);
71 }
72
73 @Override
74 public void onCreate(SQLiteDatabase db) {
75 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
76 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
77 COLUMN_PRODUCTNAME + " TEXT " +
78 ");";
79 db.execSQL(query);
80 }
81 //Lesson 51
82 @Override
83 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
84 db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
85 onCreate(db);
86 }
87
88 //Add a new row to the database
89 public void addProduct(Products product){
90 ContentValues values = new ContentValues();
91 values.put(COLUMN_PRODUCTNAME, product.get_productname());
92 SQLiteDatabase db = getWritableDatabase();
93 db.insert(TABLE_PRODUCTS, null, values);
94 db.close();
95 }
96
97 //Delete a product from the database
98 public void deleteProduct(String productName){
99 SQLiteDatabase db = getWritableDatabase();
100 db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "="" + productName + "";");
101 }
102
103 // this is goint in record_TextView in the Main activity.
104 public String databaseToString(){
105 String dbString = "";
106 SQLiteDatabase db = getWritableDatabase();
107 String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";// why not leave out the WHERE clause?
108
109 //Cursor points to a location in your results
110 Cursor recordSet = db.rawQuery(query, null);
111 //Move to the first row in your results
112 recordSet.moveToFirst();
113
114 //Position after the last row means the end of the results
115 while (!recordSet.isAfterLast()) {
116 // null could happen if we used our empty constructor
117 if (recordSet.getString(recordSet.getColumnIndex("productname")) != null) {
118 dbString += recordSet.getString(recordSet.getColumnIndex("productname"));
119 dbString += "n";
120 }
121 recordSet.moveToNext();
122 }
123 db.close();
124 return dbString;
125 }
126
127}
128
129public class Products {
130 private int _id;
131 private String _productname;
132
133 //Added this empty constructor in lesson 50 in case we ever want to create the object and assign it later.
134 public Products(){
135
136 }
137 public Products(String productName) {
138 this._productname = productName;
139 }
140
141 public int get_id() {
142 return _id;
143 }
144
145 public void set_id(int _id) {
146 this._id = _id;
147 }
148
149 public String get_productname() {
150 return _productname;
151 }
152
153 public void set_productname(String _productname) {
154 this._productname = _productname;
155 }
156}
157
158<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
159 xmlns:app="http://schemas.android.com/apk/res-auto"
160 xmlns:tools="http://schemas.android.com/tools"
161 android:layout_width="match_parent"
162 android:layout_height="match_parent"
163 tools:context=".MainActivity">
164
165 <EditText
166 android:layout_width="wrap_content"
167 android:layout_height="wrap_content"
168 android:id="@+id/user_Input"
169 android:layout_alignParentTop="true"
170 android:layout_centerHorizontal="true"
171 android:layout_marginTop="69dp"
172 android:width="300dp" />
173
174 <Button
175 android:layout_width="wrap_content"
176 android:layout_height="wrap_content"
177 android:text="Add"
178 android:id="@+id/add_Button"
179 android:layout_below="@+id/user_Input"
180 android:layout_alignStart="@+id/user_Input"
181 android:layout_marginTop="40dp"
182 android:onClick="addButtonClicked"
183 android:layout_alignLeft="@+id/user_Input" />
184
185 <Button
186 android:layout_width="wrap_content"
187 android:layout_height="wrap_content"
188 android:text="Delete"
189 android:id="@+id/delete_Button"
190 android:layout_alignTop="@+id/add_Button"
191 android:layout_alignEnd="@+id/user_Input"
192 android:onClick="deleteButtonClicked"
193 android:layout_alignRight="@+id/user_Input" />
194
195 <TextView
196 android:layout_width="wrap_content"
197 android:layout_height="wrap_content"
198 android:textAppearance="?android:attr/textAppearanceLarge"
199 android:text="Large Text"
200 android:id="@+id/records_TextView"
201 android:layout_centerVertical="true"
202 android:layout_centerHorizontal="true" />
203</RelativeLayout>