· 5 years ago · Jul 11, 2020, 07:16 AM
1//////////////////// Products List //////////////////////////
2//--------------- MainActivity.kt ----------------------
3
4import android.content.Intent
5import android.graphics.Color
6import androidx.appcompat.app.AppCompatActivity
7import android.os.Bundle
8import androidx.recyclerview.widget.LinearLayoutManager
9import kotlinx.android.synthetic.main.activity_main.*
10
11class MainActivity : AppCompatActivity() {
12
13 val dbHandler: DBHandler = DBHandler(this)
14
15 override fun onCreate(savedInstanceState: Bundle?) {
16 super.onCreate(savedInstanceState)
17 setContentView(R.layout.activity_main)
18 addProductsToDB()
19 configure()
20 }
21
22 override fun onRestart() {
23 super.onRestart()
24 configure()
25 }
26
27 private fun configure() {
28 recyclerView.layoutManager = LinearLayoutManager(this)
29 recyclerView.adapter = ProductAdapter(this)
30 btnAdd.setOnClickListener {
31 val intent = Intent(this, AddProductActivity::class.java)
32 startActivity(intent)
33 }
34 }
35
36
37 private fun addProductsToDB() {
38 dbHandler.deleteAll()
39 dbHandler.addProduct(Product(0, "table", 5))
40 dbHandler.addProduct(Product(1, "Chair", 20))
41 dbHandler.addProduct(Product(2, "Tablespoon", 200))
42 dbHandler.addProduct((Product(3, "Fork", 230)))
43 dbHandler.addProduct(Product(4, "Knife", 180))
44 }
45}
46
47//----------------------- AddProductActivity.kt ----------------------
48
49import androidx.appcompat.app.AppCompatActivity
50import android.os.Bundle
51import kotlinx.android.synthetic.main.activity_add_product.*
52
53class AddProductActivity : AppCompatActivity() {
54
55 val dbHandler: DBHandler = DBHandler(this)
56
57 override fun onCreate(savedInstanceState: Bundle?) {
58 super.onCreate(savedInstanceState)
59 setContentView(R.layout.activity_add_product)
60 setPointer()
61 }
62
63 private fun setPointer() {
64 btnAddProduct.setOnClickListener {
65 val p = Product(inputPName.text.toString(), Integer.parseInt(inputPQuantity.text.toString()))
66 dbHandler.addProduct(p)
67 finish()
68 }
69 }
70}
71
72//----------------------- DBHandler.kt ----------------------
73
74import android.content.ContentValues
75import android.content.Context
76import android.database.sqlite.SQLiteDatabase
77import android.database.sqlite.SQLiteOpenHelper
78
79class DBHandler (context: Context): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
80
81 var context: Context? = null
82
83 init {
84 this.context = context
85 }
86
87 companion object {
88 private val DATABASE_VERSION = 1
89 private val DATABASE_NAME = "productDB.db"
90 val TABLE_PRODUCTS = "products"
91
92 // Declaring columns name
93 val COLUMN_ID = "_id"
94 val COLUMN_PRODUCTNAME = "productname"
95 val COLUMN_QUANTITY = "quantity"
96
97 val CREATE_PRODUCTS_TABLE = "CREATE TABLE IF NOT EXISTS $TABLE_PRODUCTS ($COLUMN_ID INTEGER PRIMARY KEY, " +
98 "$COLUMN_PRODUCTNAME TEXT, $COLUMN_QUANTITY INTEGER)"
99 val SELECT_PRODUCT = "SELECT * FROM $TABLE_PRODUCTS"
100 }
101 override fun onCreate(db: SQLiteDatabase) {
102 db.execSQL(CREATE_PRODUCTS_TABLE)
103 }
104
105 override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
106
107 }
108
109 fun addProduct(product: Product) {
110 val values = ContentValues()
111 values.put(COLUMN_PRODUCTNAME, product.productName)
112 values.put(COLUMN_QUANTITY, product.quantity)
113
114 val db = this.writableDatabase
115 db.insert(TABLE_PRODUCTS, null, values)
116 db.close()
117 }
118
119 fun findProduct(productName: String): Product? {
120 val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
121 val db = this.readableDatabase
122 val cursor = db.rawQuery(query, null)
123 var product: Product? = null
124
125 if(cursor.moveToFirst()) {
126 cursor.moveToFirst()
127 val id = Integer.parseInt(cursor.getString(0))
128 val name = cursor.getString(1)
129 val quantity = Integer.parseInt(cursor.getString(2))
130
131 product = Product(id, name, quantity)
132 cursor.close()
133 }
134 db.close()
135 return product
136 }
137
138 fun getAllProducts(): ArrayList<Product> {
139 var products: ArrayList<Product> = ArrayList()
140 var cursor = readableDatabase.rawQuery(SELECT_PRODUCT, null)
141 if(cursor.moveToFirst()) {
142 while(!cursor.isAfterLast) {
143 val product = Product(
144 Integer.parseInt(cursor.getString(0)),
145 cursor.getString(1),
146 Integer.parseInt(cursor.getString(2))
147 )
148 products.add(product)
149 cursor.moveToNext()
150 }
151 }
152 cursor.close()
153 return products
154 }
155
156 fun deleteProduct(productName: String): Boolean {
157 var result = false
158 val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
159 val db = this.writableDatabase
160 val cursor = db.rawQuery(query, null)
161
162 if(cursor.moveToFirst()) {
163 //cursor.moveToFirst()
164 val id = Integer.parseInt(cursor.getString(0))
165 db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
166 cursor.close()
167 result = true
168 }
169 db.close()
170 return result
171 }
172
173 fun deleteAll() {
174 val db = this.writableDatabase
175 var cursor = db.rawQuery(SELECT_PRODUCT, null)
176 if(cursor.moveToFirst()) {
177 while(!cursor.isAfterLast) {
178 val id = Integer.parseInt(cursor.getString(0))
179 db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
180 cursor.moveToNext()
181 }
182 }
183 cursor.close()
184 }
185}
186
187//----------------------- Product.kt ----------------------
188
189class Product {
190 var id: Int = 0
191 var productName: String? = null
192 var quantity: Int = 0
193
194 constructor(id: Int, productName: String, quantity: Int) {
195 this.id = id
196 this.productName = productName
197 this.quantity = quantity
198 }
199
200 constructor(productName: String, quantity: Int) {
201 this.productName = productName
202 this.quantity = quantity
203 }
204}
205
206//----------------------- ProductAdapter.kt ----------------------
207
208import android.content.Context
209import android.view.LayoutInflater
210import android.view.View
211import android.view.ViewGroup
212import android.widget.Toast
213import androidx.recyclerview.widget.RecyclerView
214import kotlinx.android.synthetic.main.product_layout.view.*
215
216class ProductAdapter(private val context: Context): RecyclerView.Adapter<ProductViewHolder>() {
217
218 val dbHandler: DBHandler = DBHandler(context)
219
220 private var productsName = dbHandler.getAllProducts().map { it.productName }
221 private var productsAmount = dbHandler.getAllProducts().map { it.quantity.toString() }
222
223 override fun getItemCount(): Int {
224 return productsAmount.count()
225 }
226
227 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
228 val layoutInflater = LayoutInflater.from(parent?.context)
229 val cellForRow = layoutInflater.inflate(R.layout.product_layout, parent, false)
230 return ProductViewHolder(cellForRow)
231 }
232
233 override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
234 holder.view.name.text = productsName[position]
235 holder.view.amount.text = productsAmount[position]
236 holder.view.setOnClickListener {
237 val name = productsName[position].toString()
238 dbHandler.deleteProduct(name)
239 productsName = dbHandler.getAllProducts().map { it.productName }
240 productsAmount = dbHandler.getAllProducts().map { it.quantity.toString() }
241 notifyDataSetChanged()
242 }
243 }
244}
245
246class ProductViewHolder(val view: View): RecyclerView.ViewHolder(view) { }
247
248//----------------------- activity_main.xml ----------------------
249
250<?xml version="1.0" encoding="utf-8"?>
251<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
252 xmlns:app="http://schemas.android.com/apk/res-auto"
253 xmlns:tools="http://schemas.android.com/tools"
254 android:layout_width="match_parent"
255 android:layout_height="match_parent"
256 tools:context=".MainActivity">
257
258 <androidx.recyclerview.widget.RecyclerView
259 android:id="@+id/recyclerView"
260 android:layout_width="378dp"
261 android:layout_height="507dp"
262 android:layout_marginTop="70dp"
263 app:layout_constraintBottom_toBottomOf="parent"
264 app:layout_constraintEnd_toEndOf="parent"
265 app:layout_constraintStart_toStartOf="parent"
266 app:layout_constraintTop_toTopOf="parent" />
267
268 <TextView
269 android:id="@+id/textView2"
270 android:layout_width="227dp"
271 android:layout_height="51dp"
272 android:text="@string/products"
273 android:textAlignment="center"
274 android:textAllCaps="true"
275 android:textSize="30sp"
276 android:textStyle="bold|italic"
277 app:layout_constraintBottom_toTopOf="@+id/recyclerView"
278 app:layout_constraintEnd_toEndOf="parent"
279 app:layout_constraintStart_toStartOf="parent"
280 app:layout_constraintTop_toTopOf="parent" />
281
282 <Button
283 android:id="@+id/btnAdd"
284 android:layout_width="62dp"
285 android:layout_height="60dp"
286 android:text="@string/add"
287 android:textSize="30sp"
288 app:layout_constraintBottom_toTopOf="@+id/recyclerView"
289 app:layout_constraintEnd_toEndOf="parent"
290 app:layout_constraintStart_toEndOf="@+id/textView2"
291 app:layout_constraintTop_toTopOf="parent" />
292
293</androidx.constraintlayout.widget.ConstraintLayout>
294
295//----------------------- activity_add_product.xml ----------------------
296
297<?xml version="1.0" encoding="utf-8"?>
298<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
299 xmlns:app="http://schemas.android.com/apk/res-auto"
300 xmlns:tools="http://schemas.android.com/tools"
301 android:layout_width="match_parent"
302 android:layout_height="match_parent"
303 tools:context=".AddProductActivity">
304
305 <TextView
306 android:id="@+id/addProductTxt"
307 android:layout_width="215dp"
308 android:layout_height="47dp"
309 android:text="@string/add_product"
310 android:textAlignment="center"
311 android:textAllCaps="true"
312 android:textSize="30sp"
313 android:textStyle="bold|italic"
314 app:layout_constraintBottom_toBottomOf="parent"
315 app:layout_constraintEnd_toEndOf="parent"
316 app:layout_constraintStart_toStartOf="parent"
317 app:layout_constraintTop_toTopOf="parent"
318 app:layout_constraintVertical_bias="0.136" />
319
320 <TextView
321 android:id="@+id/titleTxt"
322 android:layout_width="118dp"
323 android:layout_height="36dp"
324 android:text="@string/product_title"
325 android:textSize="18sp"
326 android:textStyle="bold|italic"
327 app:layout_constraintBottom_toBottomOf="parent"
328 app:layout_constraintEnd_toEndOf="parent"
329 app:layout_constraintHorizontal_bias="0.17"
330 app:layout_constraintStart_toStartOf="parent"
331 app:layout_constraintTop_toBottomOf="@+id/addProductTxt"
332 app:layout_constraintVertical_bias="0.168" />
333
334 <TextView
335 android:id="@+id/quantityTxt"
336 android:layout_width="118dp"
337 android:layout_height="36dp"
338 android:text="@string/quantity"
339 android:textSize="18sp"
340 android:textStyle="bold|italic"
341 app:layout_constraintBottom_toBottomOf="parent"
342 app:layout_constraintEnd_toEndOf="parent"
343 app:layout_constraintHorizontal_bias="0.17"
344 app:layout_constraintStart_toStartOf="parent"
345 app:layout_constraintTop_toBottomOf="@+id/titleTxt"
346 app:layout_constraintVertical_bias="0.086" />
347
348 <EditText
349 android:id="@+id/inputPName"
350 android:layout_width="179dp"
351 android:layout_height="42dp"
352 android:ems="10"
353 android:hint="@string/title"
354 android:inputType="textPersonName"
355 android:labelFor="@id/titleTxt"
356 app:layout_constraintCircle="@id/titleTxt"
357 app:layout_constraintCircleAngle="90"
358 app:layout_constraintCircleRadius="170dp"
359 app:layout_constraintEnd_toEndOf="parent"
360 app:layout_constraintStart_toEndOf="@+id/titleTxt"
361 app:layout_constraintTop_toBottomOf="@+id/addProductTxt"
362 android:autofillHints="Title" />
363
364 <EditText
365 android:id="@+id/inputPQuantity"
366 android:layout_width="179dp"
367 android:layout_height="42dp"
368 android:ems="10"
369 android:hint="@string/quantity_add"
370 android:inputType="textPersonName"
371 app:layout_constraintBottom_toBottomOf="parent"
372 app:layout_constraintCircle="@id/quantityTxt"
373 app:layout_constraintCircleAngle="90"
374 app:layout_constraintCircleRadius="170dp"
375 app:layout_constraintEnd_toEndOf="parent"
376 app:layout_constraintStart_toEndOf="@+id/quantityTxt"
377 app:layout_constraintTop_toBottomOf="@+id/inputPName"
378 android:autofillHints="Quantity" />
379
380 <Button
381 android:id="@+id/btnAddProduct"
382 android:layout_width="wrap_content"
383 android:layout_height="wrap_content"
384 android:text="@string/add_btn"
385 app:layout_constraintBottom_toBottomOf="parent"
386 app:layout_constraintEnd_toEndOf="parent"
387 app:layout_constraintHorizontal_bias="0.498"
388 app:layout_constraintStart_toStartOf="parent"
389 app:layout_constraintTop_toBottomOf="@+id/inputPQuantity"
390 app:layout_constraintVertical_bias="0.212" />
391
392</androidx.constraintlayout.widget.ConstraintLayout>
393
394//----------------------- product_layout.xml ----------------------
395
396<?xml version="1.0" encoding="utf-8"?>
397<androidx.constraintlayout.widget.ConstraintLayout
398 xmlns:android="http://schemas.android.com/apk/res/android"
399 xmlns:app="http://schemas.android.com/apk/res-auto"
400 xmlns:tools="http://schemas.android.com/tools"
401 android:layout_width="match_parent"
402 android:layout_height="wrap_content">
403
404 <ImageView
405 android:id="@+id/imageView"
406 android:layout_width="81dp"
407 android:layout_height="68dp"
408 android:layout_marginTop="8dp"
409 app:layout_constraintStart_toStartOf="parent"
410 app:layout_constraintTop_toTopOf="parent"
411 app:srcCompat="@android:drawable/btn_star_big_on" />
412
413 <TextView
414 android:id="@+id/name"
415 android:layout_width="240dp"
416 android:layout_height="34dp"
417 android:layout_marginStart="24dp"
418 android:layout_marginTop="8dp"
419 android:layout_marginEnd="60dp"
420 android:text="name"
421 android:textSize="24sp"
422 app:layout_constraintEnd_toEndOf="parent"
423 app:layout_constraintStart_toEndOf="@+id/imageView"
424 app:layout_constraintTop_toTopOf="parent" />
425
426 <TextView
427 android:id="@+id/amount"
428 android:layout_width="240dp"
429 android:layout_height="23dp"
430 android:layout_marginStart="24dp"
431 android:layout_marginTop="11dp"
432 android:layout_marginEnd="60dp"
433 android:text="amount"
434 app:layout_constraintBottom_toBottomOf="@+id/imageView"
435 app:layout_constraintEnd_toEndOf="parent"
436 app:layout_constraintStart_toEndOf="@+id/imageView"
437 app:layout_constraintTop_toBottomOf="@+id/name" />
438</androidx.constraintlayout.widget.ConstraintLayout>
439
440//----------------------- strings.xml ----------------------
441
442<resources>
443 <string name="app_name">Products</string>
444 <string name="products">Products</string>
445 <string name="add">+</string>
446 <string name="add_product">Add Product</string>
447 <string name="product_title">Product Title:</string>
448 <string name="quantity">Quantity:</string>
449 <string name="quantity_add">Quantity</string>
450 <string name="title">Title</string>
451 <string name="add_btn">Add</string>
452</resources>
453
454//----------------------- build.gradle (Module:app) ----------------------
455
456implementation 'androidx.recyclerview:recyclerview:1.1.0' //JUST ADD THIS LINE TO THE DEPENDENCIES