Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////// Products List //////////////////////////
- //--------------- MainActivity.kt ----------------------
- import android.content.Intent
- import android.graphics.Color
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import androidx.recyclerview.widget.LinearLayoutManager
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- val dbHandler: DBHandler = DBHandler(this)
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- addProductsToDB()
- configure()
- }
- override fun onRestart() {
- super.onRestart()
- configure()
- }
- private fun configure() {
- recyclerView.layoutManager = LinearLayoutManager(this)
- recyclerView.adapter = ProductAdapter(this)
- btnAdd.setOnClickListener {
- val intent = Intent(this, AddProductActivity::class.java)
- startActivity(intent)
- }
- }
- private fun addProductsToDB() {
- dbHandler.deleteAll()
- dbHandler.addProduct(Product(0, "table", 5))
- dbHandler.addProduct(Product(1, "Chair", 20))
- dbHandler.addProduct(Product(2, "Tablespoon", 200))
- dbHandler.addProduct((Product(3, "Fork", 230)))
- dbHandler.addProduct(Product(4, "Knife", 180))
- }
- }
- //----------------------- AddProductActivity.kt ----------------------
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import kotlinx.android.synthetic.main.activity_add_product.*
- class AddProductActivity : AppCompatActivity() {
- val dbHandler: DBHandler = DBHandler(this)
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_add_product)
- setPointer()
- }
- private fun setPointer() {
- btnAddProduct.setOnClickListener {
- val p = Product(inputPName.text.toString(), Integer.parseInt(inputPQuantity.text.toString()))
- dbHandler.addProduct(p)
- finish()
- }
- }
- }
- //----------------------- DBHandler.kt ----------------------
- import android.content.ContentValues
- import android.content.Context
- import android.database.sqlite.SQLiteDatabase
- import android.database.sqlite.SQLiteOpenHelper
- class DBHandler (context: Context): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
- var context: Context? = null
- init {
- this.context = context
- }
- companion object {
- private val DATABASE_VERSION = 1
- private val DATABASE_NAME = "productDB.db"
- val TABLE_PRODUCTS = "products"
- // Declaring columns name
- val COLUMN_ID = "_id"
- val COLUMN_PRODUCTNAME = "productname"
- val COLUMN_QUANTITY = "quantity"
- val CREATE_PRODUCTS_TABLE = "CREATE TABLE IF NOT EXISTS $TABLE_PRODUCTS ($COLUMN_ID INTEGER PRIMARY KEY, " +
- "$COLUMN_PRODUCTNAME TEXT, $COLUMN_QUANTITY INTEGER)"
- val SELECT_PRODUCT = "SELECT * FROM $TABLE_PRODUCTS"
- }
- override fun onCreate(db: SQLiteDatabase) {
- db.execSQL(CREATE_PRODUCTS_TABLE)
- }
- override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
- }
- fun addProduct(product: Product) {
- val values = ContentValues()
- values.put(COLUMN_PRODUCTNAME, product.productName)
- values.put(COLUMN_QUANTITY, product.quantity)
- val db = this.writableDatabase
- db.insert(TABLE_PRODUCTS, null, values)
- db.close()
- }
- fun findProduct(productName: String): Product? {
- val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
- val db = this.readableDatabase
- val cursor = db.rawQuery(query, null)
- var product: Product? = null
- if(cursor.moveToFirst()) {
- cursor.moveToFirst()
- val id = Integer.parseInt(cursor.getString(0))
- val name = cursor.getString(1)
- val quantity = Integer.parseInt(cursor.getString(2))
- product = Product(id, name, quantity)
- cursor.close()
- }
- db.close()
- return product
- }
- fun getAllProducts(): ArrayList<Product> {
- var products: ArrayList<Product> = ArrayList()
- var cursor = readableDatabase.rawQuery(SELECT_PRODUCT, null)
- if(cursor.moveToFirst()) {
- while(!cursor.isAfterLast) {
- val product = Product(
- Integer.parseInt(cursor.getString(0)),
- cursor.getString(1),
- Integer.parseInt(cursor.getString(2))
- )
- products.add(product)
- cursor.moveToNext()
- }
- }
- cursor.close()
- return products
- }
- fun deleteProduct(productName: String): Boolean {
- var result = false
- val query = "$SELECT_PRODUCT WHERE $COLUMN_PRODUCTNAME = \"$productName\""
- val db = this.writableDatabase
- val cursor = db.rawQuery(query, null)
- if(cursor.moveToFirst()) {
- //cursor.moveToFirst()
- val id = Integer.parseInt(cursor.getString(0))
- db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
- cursor.close()
- result = true
- }
- db.close()
- return result
- }
- fun deleteAll() {
- val db = this.writableDatabase
- var cursor = db.rawQuery(SELECT_PRODUCT, null)
- if(cursor.moveToFirst()) {
- while(!cursor.isAfterLast) {
- val id = Integer.parseInt(cursor.getString(0))
- db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
- cursor.moveToNext()
- }
- }
- cursor.close()
- }
- }
- //----------------------- Product.kt ----------------------
- class Product {
- var id: Int = 0
- var productName: String? = null
- var quantity: Int = 0
- constructor(id: Int, productName: String, quantity: Int) {
- this.id = id
- this.productName = productName
- this.quantity = quantity
- }
- constructor(productName: String, quantity: Int) {
- this.productName = productName
- this.quantity = quantity
- }
- }
- //----------------------- ProductAdapter.kt ----------------------
- import android.content.Context
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.widget.Toast
- import androidx.recyclerview.widget.RecyclerView
- import kotlinx.android.synthetic.main.product_layout.view.*
- class ProductAdapter(private val context: Context): RecyclerView.Adapter<ProductViewHolder>() {
- val dbHandler: DBHandler = DBHandler(context)
- private var productsName = dbHandler.getAllProducts().map { it.productName }
- private var productsAmount = dbHandler.getAllProducts().map { it.quantity.toString() }
- override fun getItemCount(): Int {
- return productsAmount.count()
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
- val layoutInflater = LayoutInflater.from(parent?.context)
- val cellForRow = layoutInflater.inflate(R.layout.product_layout, parent, false)
- return ProductViewHolder(cellForRow)
- }
- override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
- holder.view.name.text = productsName[position]
- holder.view.amount.text = productsAmount[position]
- holder.view.setOnClickListener {
- val name = productsName[position].toString()
- dbHandler.deleteProduct(name)
- productsName = dbHandler.getAllProducts().map { it.productName }
- productsAmount = dbHandler.getAllProducts().map { it.quantity.toString() }
- notifyDataSetChanged()
- }
- }
- }
- class ProductViewHolder(val view: View): RecyclerView.ViewHolder(view) { }
- //----------------------- activity_main.xml ----------------------
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recyclerView"
- android:layout_width="378dp"
- android:layout_height="507dp"
- android:layout_marginTop="70dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="227dp"
- android:layout_height="51dp"
- android:text="@string/products"
- android:textAlignment="center"
- android:textAllCaps="true"
- android:textSize="30sp"
- android:textStyle="bold|italic"
- app:layout_constraintBottom_toTopOf="@+id/recyclerView"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="62dp"
- android:layout_height="60dp"
- android:text="@string/add"
- android:textSize="30sp"
- app:layout_constraintBottom_toTopOf="@+id/recyclerView"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/textView2"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- //----------------------- activity_add_product.xml ----------------------
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".AddProductActivity">
- <TextView
- android:id="@+id/addProductTxt"
- android:layout_width="215dp"
- android:layout_height="47dp"
- android:text="@string/add_product"
- android:textAlignment="center"
- android:textAllCaps="true"
- android:textSize="30sp"
- android:textStyle="bold|italic"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.136" />
- <TextView
- android:id="@+id/titleTxt"
- android:layout_width="118dp"
- android:layout_height="36dp"
- android:text="@string/product_title"
- android:textSize="18sp"
- android:textStyle="bold|italic"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.17"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/addProductTxt"
- app:layout_constraintVertical_bias="0.168" />
- <TextView
- android:id="@+id/quantityTxt"
- android:layout_width="118dp"
- android:layout_height="36dp"
- android:text="@string/quantity"
- android:textSize="18sp"
- android:textStyle="bold|italic"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.17"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/titleTxt"
- app:layout_constraintVertical_bias="0.086" />
- <EditText
- android:id="@+id/inputPName"
- android:layout_width="179dp"
- android:layout_height="42dp"
- android:ems="10"
- android:hint="@string/title"
- android:inputType="textPersonName"
- android:labelFor="@id/titleTxt"
- app:layout_constraintCircle="@id/titleTxt"
- app:layout_constraintCircleAngle="90"
- app:layout_constraintCircleRadius="170dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/titleTxt"
- app:layout_constraintTop_toBottomOf="@+id/addProductTxt"
- android:autofillHints="Title" />
- <EditText
- android:id="@+id/inputPQuantity"
- android:layout_width="179dp"
- android:layout_height="42dp"
- android:ems="10"
- android:hint="@string/quantity_add"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintCircle="@id/quantityTxt"
- app:layout_constraintCircleAngle="90"
- app:layout_constraintCircleRadius="170dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/quantityTxt"
- app:layout_constraintTop_toBottomOf="@+id/inputPName"
- android:autofillHints="Quantity" />
- <Button
- android:id="@+id/btnAddProduct"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/add_btn"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/inputPQuantity"
- app:layout_constraintVertical_bias="0.212" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- //----------------------- product_layout.xml ----------------------
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="81dp"
- android:layout_height="68dp"
- android:layout_marginTop="8dp"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@android:drawable/btn_star_big_on" />
- <TextView
- android:id="@+id/name"
- android:layout_width="240dp"
- android:layout_height="34dp"
- android:layout_marginStart="24dp"
- android:layout_marginTop="8dp"
- android:layout_marginEnd="60dp"
- android:text="name"
- android:textSize="24sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/imageView"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/amount"
- android:layout_width="240dp"
- android:layout_height="23dp"
- android:layout_marginStart="24dp"
- android:layout_marginTop="11dp"
- android:layout_marginEnd="60dp"
- android:text="amount"
- app:layout_constraintBottom_toBottomOf="@+id/imageView"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/imageView"
- app:layout_constraintTop_toBottomOf="@+id/name" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- //----------------------- strings.xml ----------------------
- <resources>
- <string name="app_name">Products</string>
- <string name="products">Products</string>
- <string name="add">+</string>
- <string name="add_product">Add Product</string>
- <string name="product_title">Product Title:</string>
- <string name="quantity">Quantity:</string>
- <string name="quantity_add">Quantity</string>
- <string name="title">Title</string>
- <string name="add_btn">Add</string>
- </resources>
- //----------------------- build.gradle (Module:app) ----------------------
- implementation 'androidx.recyclerview:recyclerview:1.1.0' //JUST ADD THIS LINE TO THE DEPENDENCIES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement