· 5 years ago · Jul 08, 2020, 06:06 PM
1//------------- Product.kt ----------------------
2
3class Product {
4 var id: Int = 0
5 var productName: String? = null
6 var quantity: Int = 0
7
8 constructor(id: Int, productName: String, quantity: Int) {
9 this.id = id
10 this.productName = productName
11 this.quantity = quantity
12 }
13
14 constructor(productName: String, quantity: Int) {
15 this.productName = productName
16 this.quantity = quantity
17 }
18}
19
20//---------------- DBHandler.kt -------------------
21
22import android.content.ContentValues
23import android.content.Context
24import android.database.sqlite.SQLiteDatabase
25import android.database.sqlite.SQLiteOpenHelper
26import androidx.annotation.IntegerRes
27
28class DBHandler (context: Context): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
29
30 var context: Context? = null
31
32 init {
33 this.context = context
34 }
35
36 companion object {
37 private val DATABASE_VERSION = 1
38 private val DATABASE_NAME = "productDB.db"
39 val TABLE_PRODUCTS = "products"
40
41 // Declaring columns name
42 val COLUMN_ID = "_id"
43 val COLUMN_PRODUCTNAME = "productname"
44 val COLUMN_QUANTITY = "quantity"
45
46 val CREATE_PRODUCTS_TABLE = "CREATE TABLE IF NOT EXISTS $TABLE_PRODUCTS ($COLUMN_ID INTEGER PRIMARY KEY, " +
47 "$COLUMN_PRODUCTNAME TEXT, $COLUMN_QUANTITY INTEGER)"
48 val SELECT_PRODUCT = "SELECT * FROM $TABLE_PRODUCTS"
49 }
50 override fun onCreate(db: SQLiteDatabase) {
51 db.execSQL(CREATE_PRODUCTS_TABLE)
52 }
53
54 override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
55
56 }
57
58 fun addProduct(product: Product) {
59 val values = ContentValues()
60 values.put(COLUMN_PRODUCTNAME, product.productName)
61 values.put(COLUMN_QUANTITY, product.quantity)
62
63 val db = this.writableDatabase
64 db.insert(TABLE_PRODUCTS, null, values)
65 db.close()
66 }
67
68 fun findProduct(productName: String): Product? {
69 val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
70 val db = this.readableDatabase
71 val cursor = db.rawQuery(query, null)
72 var product: Product? = null
73
74 if(cursor.moveToFirst()) {
75 cursor.moveToFirst()
76 val id = Integer.parseInt(cursor.getString(0))
77 val name = cursor.getString(1)
78 val quantity = Integer.parseInt(cursor.getString(2))
79
80 product = Product(id, name, quantity)
81 cursor.close()
82 }
83 db.close()
84 return product
85 }
86
87 fun getAllProducts(): ArrayList<Product> {
88 var products: ArrayList<Product> = ArrayList()
89 var cursor = readableDatabase.rawQuery(SELECT_PRODUCT, null)
90 if(cursor.moveToFirst()) {
91 while(!cursor.isAfterLast) {
92 val product = Product(
93 Integer.parseInt(cursor.getString(0)),
94 cursor.getString(1),
95 Integer.parseInt(cursor.getString(2))
96 )
97 products.add(product)
98 cursor.moveToNext()
99 }
100 }
101 cursor.close()
102 return products
103 }
104
105 fun deleteProduct(productName: String): Boolean {
106 var result = false
107 val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
108 val db = this.writableDatabase
109 val cursor = db.rawQuery(query, null)
110
111 if(cursor.moveToFirst()) {
112 //cursor.moveToFirst()
113 val id = Integer.parseInt(cursor.getString(0))
114 db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
115 cursor.close()
116 result = true
117 }
118 db.close()
119 return result
120 }
121
122 fun deleteAll() {
123 val db = this.writableDatabase
124 var cursor = db.rawQuery(SELECT_PRODUCT, null)
125 if(cursor.moveToFirst()) {
126 while(!cursor.isAfterLast) {
127 val id = Integer.parseInt(cursor.getString(0))
128 db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
129 cursor.moveToNext()
130 }
131 }
132 cursor.close()
133 }
134}
135
136//-------------------- MainActivity.kt ----------------------
137
138import androidx.appcompat.app.AppCompatActivity
139import android.os.Bundle
140import android.widget.ArrayAdapter
141import kotlinx.android.synthetic.main.activity_main.*
142
143class MainActivity : AppCompatActivity() {
144
145 val dbHandler: DBHandler = DBHandler(this)
146
147 lateinit var stringsList: ArrayList<String>
148
149 override fun onCreate(savedInstanceState: Bundle?) {
150 super.onCreate(savedInstanceState)
151 setContentView(R.layout.activity_main)
152 setPointer()
153 }
154
155 private fun setPointer() {
156 addProductsToDB()
157 setAdapter()
158 }
159
160 private fun setAdapter() {
161 val products = dbHandler.getAllProducts()
162 stringsList = ArrayList()
163 for (product in products) {
164 stringsList.add("${product.id} ${product.productName} ${product.quantity}")
165 }
166 products_list.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, stringsList)
167 }
168
169 private fun addProductsToDB() {
170 dbHandler.deleteAll()
171 dbHandler.addProduct(Product(0, "table", 5))
172 dbHandler.addProduct(Product(1, "Chair", 20))
173 dbHandler.addProduct(Product(2, "Tablespoon", 200))
174 dbHandler.addProduct((Product(3, "Fork", 230)))
175 dbHandler.addProduct(Product(4, "Knife", 180))
176 }
177}
178
179//------------------ activity_main.xml ---------------------
180
181<?xml version="1.0" encoding="utf-8"?>
182<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
183 xmlns:app="http://schemas.android.com/apk/res-auto"
184 xmlns:tools="http://schemas.android.com/tools"
185 android:id="@+id/productsList"
186 android:layout_width="match_parent"
187 android:layout_height="match_parent"
188 tools:context=".MainActivity">
189
190 <TextView
191 android:id="@+id/textView"
192 android:layout_width="wrap_content"
193 android:layout_height="wrap_content"
194 android:text="Products"
195 android:textSize="24sp"
196 app:layout_constraintBottom_toBottomOf="parent"
197 app:layout_constraintLeft_toLeftOf="parent"
198 app:layout_constraintRight_toRightOf="parent"
199 app:layout_constraintTop_toTopOf="parent"
200 app:layout_constraintVertical_bias="0.095" />
201
202 <ListView
203 android:id="@+id/products_list"
204 android:layout_width="359dp"
205 android:layout_height="431dp"
206 android:layout_marginStart="20dp"
207 android:layout_marginTop="30dp"
208 android:layout_marginEnd="20dp"
209 android:layout_marginBottom="30dp"
210 app:layout_constraintBottom_toBottomOf="parent"
211 app:layout_constraintEnd_toEndOf="parent"
212 app:layout_constraintStart_toStartOf="parent"
213 app:layout_constraintTop_toBottomOf="@+id/textView" />
214
215</androidx.constraintlayout.widget.ConstraintLayout>