· 8 years ago · Apr 22, 2018, 07:52 AM
1package com.example.frankienglish.sqlitesample;
2
3import android.support.v7.app.AppCompatActivity;
4import android.os.Bundle;
5import android.view.Menu;
6import android.view.MenuItem;
7import android.view.View;
8import android.widget.EditText;
9import android.widget.TextView;
10
11public class MainActivity extends AppCompatActivity {
12
13EditText frankiesInput;
14TextView frankiesText;
15MyDBHandler dbHandler;
16@Override
17protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_main);
20
21 frankiesInput = (EditText) findViewById(R.id.frankiesInput);
22 frankiesText = (TextView) findViewById(R.id.frankiesInput);
23 dbHandler = new MyDBHandler(this, null, null, 1);
24 printDatabase();
25}
26
27//add a product to the databse
28public void addButtonClicked(View view){
29 Products product = new Products(frankiesInput.getText().toString());
30 dbHandler.addProduct(product);
31 printDatabase();
32}
33
34//DeleteItems
35public void deleteButtonClicked(View view){
36 String inputText = frankiesInput.getText().toString();
37 dbHandler.deleteProduct(inputText);
38 printDatabase();
39
40}
41
42public void printDatabase(){
43 String dbString = dbHandler.databaseToString();
44 frankiesText.setText(dbString);
45 frankiesInput.setText("");
46
47}
48
49}
50
51public class Products
52{
53
54 private int _id;
55 private String _productname;
56
57
58 public Products(){}
59
60 public Products(String productname) {
61 this._productname = productname;
62 }
63
64 public void set_id(int _id) {
65 this._id = _id;
66 }
67
68 public void set_productname(String _productname) {
69 this._productname = _productname;
70 }
71
72 public String get_productname() {
73 return _productname;
74 }
75
76 public int get_id() {
77 return _id;
78 }
79}
80
81public class MyDBHandler extends SQLiteOpenHelper {
82
83 private static final int DATABASE_VERSION = 1; // version
84 private static final String DATABASE_NAME = "products.db"; // file saving on the device
85 public static final String TABLE_PRODUCTS = "products"; // name of table
86 public static final String COLUMN_ID = "_id"; // columns in the table
87 public static final String COLUMN_PRODUCTNAME = "productname";
88
89 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { // housekeeping
90 super(context, DATABASE_NAME, factory, DATABASE_VERSION);
91 }
92
93 @Override
94 public void onCreate(SQLiteDatabase db) { // called the first time it is created
95 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
96 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
97 COLUMN_PRODUCTNAME + " TEXT " +
98 ");";
99 db.execSQL(query); // executes the query that we just created
100 }
101
102 @Override
103 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // called when you upgrade your version
104 db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS); // deletes the table
105 onCreate(db);
106 // delete old structure and create new one
107 }
108
109 // add a new row to the database
110 public void addProduct(Products product){
111 ContentValues values = new ContentValues();
112 values.put(COLUMN_PRODUCTNAME, product.get_productname());
113 SQLiteDatabase db = getWritableDatabase(); // the database that we are going to write to
114 db.insert(TABLE_PRODUCTS, null, values); // inserts new product
115 db.close();
116 }
117
118 // Delete a product from the databse
119 public void deleteProduct(String productName){
120 SQLiteDatabase db = getWritableDatabase(); // the database that we are going to write to
121 db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "="" + productName + "";");
122 }
123
124 // Print out the databse as a string
125 public String databaseToString(){
126 String dbString = "";
127 SQLiteDatabase db = getWritableDatabase();
128 String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
129
130 //Cursor point to a location in your results
131 Cursor c = db.rawQuery(query, null);
132 // move to the first row in your results
133 c.moveToFirst();
134
135
136 while (!c.isAfterLast()){ // make sure its not positioned after the last row
137 if (c.getString(c.getColumnIndex("productname"))!=null){ // evertime it loops extracting the name of the product and then moving to a new line
138 dbString += c.getString(c.getColumnIndex("productname"));
139 dbString += "n";
140 }
141 }
142 db.close();
143 return dbString;
144 }
145
146}
147
148<?xml version="1.0" encoding="utf-8"?>
149<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
150 xmlns:app="http://schemas.android.com/apk/res-auto"
151 xmlns:tools="http://schemas.android.com/tools"
152 android:layout_width="match_parent"
153 android:layout_height="match_parent"
154 tools:context=".MainActivity">
155
156 <EditText
157 android:id="@+id/frankiesInput"
158 android:layout_width="wrap_content"
159 android:layout_height="wrap_content"
160 android:layout_alignParentTop="true"
161 android:layout_centerHorizontal="true"
162 android:layout_marginTop="39dp"
163 android:ems="10"
164 android:inputType="textPersonName" />
165
166 <Button
167 android:id="@+id/addButton"
168 android:layout_width="wrap_content"
169 android:layout_height="wrap_content"
170 android:layout_alignParentTop="true"
171 android:layout_marginTop="107dp"
172 android:layout_toStartOf="@+id/frankiesText"
173 android:layout_toLeftOf="@+id/frankiesText"
174 android:onClick="addButtonClicked"
175 android:text="Add" />
176
177 <Button
178 android:id="@+id/deleteButton"
179 android:layout_width="wrap_content"
180 android:layout_height="wrap_content"
181 android:layout_alignEnd="@+id/frankiesInput"
182 android:layout_alignRight="@+id/frankiesInput"
183 android:layout_alignTop="@+id/addButton"
184 android:onClick="deleteButtonClicked"
185 android:text="Delete" />
186
187 <TextView
188 android:id="@+id/frankiesText"
189 android:layout_width="wrap_content"
190 android:layout_height="wrap_content"
191 android:layout_alignParentTop="true"
192 android:layout_centerHorizontal="true"
193 android:layout_marginTop="188dp"
194 android:text="TextView" />