Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
Total Marks: 04
Obtained Marks:
Android App Development
Assignment # 03
Teacher Name: Sir Zubair Ahmad
_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Student Name: Muhammad Aziz Niazi
______________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Reg. Number: 2212201
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
Assignment 3
package com.example.databaseappp
data class CustomerModel(
var id: Int,
var name: String,
var age: String,
var isActive: Boolean
){
override fun toString(): String {
return "Id>>> $id\nName >>> $name\nAge>>> $age \nisActive>>> $isActive"
}
}
package com.example.databaseappp
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class DatabaseSqlLite(context: Context) :
SQLiteOpenHelper(context, "customer.db", null, 2) {
companion object {
const val CUSTOMER_TABLE = "CUSTOMER_TABLE"
const val COLUMN_ID = "ID"
const val COLUMN_CUSTOMER_NAME = "CUSTOMER_NAME"
const val COLUMN_CUSTOMER_AGE = "CUSTOMER_AGE"
const val COLUMN_ACTIVE_CUSTOMER = "ACTIVE_CUSTOMER"
}
override fun onCreate(db: SQLiteDatabase?) {
val createTableStatement = """
CREATE TABLE $CUSTOMER_TABLE (
$COLUMN_ID INTEGER PRIMARY KEY ,
$COLUMN_CUSTOMER_NAME TEXT NOT NULL,
$COLUMN_CUSTOMER_AGE INTEGER NOT NULL,
$COLUMN_ACTIVE_CUSTOMER INTEGER NOT NULL
)
""".trimIndent()
db?.execSQL(createTableStatement)
}
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS $CUSTOMER_TABLE")
onCreate(db)
}
fun addOne(customer: CustomerModel): Boolean {
val db = this.writableDatabase
val cv = ContentValues()
cv.put(COLUMN_CUSTOMER_NAME, customer.name)
cv.put(COLUMN_CUSTOMER_AGE, customer.age.toIntOrNull() ?: 0)
cv.put(COLUMN_ACTIVE_CUSTOMER, if (customer.isActive) 1 else 0)
val insert = db.insert(CUSTOMER_TABLE, null, cv)
db.close()
return insert != -1L
}
fun getAllCustomers(): List<CustomerModel> {
val returnList = mutableListOf<CustomerModel>()
val db = this.readableDatabase
val query = "SELECT * FROM $CUSTOMER_TABLE"
val cursor = db.rawQuery(query, null)
if (cursor.moveToFirst()) {
do {
val id = cursor.getInt(0)
val name = cursor.getString(1)
val age = cursor.getInt(2).toString()
val isActive = cursor.getInt(3) == 1
returnList.add(CustomerModel(id, name, age, isActive))
} while (cursor.moveToNext())
}
cursor.close()
db.close()
return returnList
}
fun clearAllCustomers(): Boolean {
val db = writableDatabase
val result = db.delete(CUSTOMER_TABLE, null, null)
db.close()
return result > 0
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
package com.example.databaseappp
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var Add: Button
lateinit var ViewAll: Button
lateinit var CustomerName: EditText
lateinit var CustomerAge: EditText
lateinit var StatusSwitch: Switch
lateinit var ListSection: ListView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
// View references
Add = findViewById(R.id.AddButton)
ViewAll = findViewById(R.id.ViewAll)
CustomerName = findViewById(R.id.CustomerName)
CustomerAge = findViewById(R.id.CustomerAge)
StatusSwitch = findViewById(R.id.StatusSwitch)
ListSection = findViewById(R.id.ListSection)
Add.setOnClickListener { AddonClick(it) }
ViewAll.setOnClickListener { ViewAllClick(it) }
}
fun ViewAllClick(view: View) {
val databaseHelper = DatabaseSqlLite(this)
val everyone: List<CustomerModel> = databaseHelper.getAllCustomers()
// Create and set up the ArrayAdapter
val customerArrayAdapter = ArrayAdapter<CustomerModel>(
this, // Context
android.R.layout.simple_list_item_1, // Layout for each item
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
everyone // List of CustomerModel objects
)
// Set the adapter to your ListView
ListSection.adapter = customerArrayAdapter
//Toast.makeText(this, everyone.toString(), Toast.LENGTH_LONG).show()
}
fun AddonClick(view: View)
{
val name = CustomerName.text.toString()
val ageStr = CustomerAge.text.toString()
val age = if (ageStr.isNotEmpty()) ageStr else "0"
val isActive = StatusSwitch.isChecked
val model = CustomerModel(
id = 0,
name = name,
age = age,
isActive = isActive
)
Toast.makeText(this, model.toString(), Toast.LENGTH_SHORT).show()
val databaseHelper = DatabaseSqlLite(this)
val success = databaseHelper.addOne(model)
Toast.makeText(this, "Success = $success", Toast.LENGTH_SHORT).show()
}
fun Deletedatabase(view: View){
val db = DatabaseSqlLite(this)
val success = db.clearAllCustomers()
Toast.makeText(this, if (success) "Database cleared" else "Nothing to delete",
Toast.LENGTH_SHORT).show()
}
}
XML Code
<?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"
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/CustomerAge"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="24dp"
android:background="@drawable/rounded_edittext"
android:elegantTextHeight="true"
android:ems="10"
android:gravity="center_vertical"
android:hint="Age"
android:inputType="number"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/CustomerName" />
<Button
android:id="@+id/AddButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:backgroundTint="#089EED"
android:onClick="AddonClick"
android:text="Add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/CustomerAge" />
<Button
android:id="@+id/ViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:backgroundTint="#03A9F4"
android:onClick="ViewAllClick"
android:text="View All"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.303"
app:layout_constraintStart_toEndOf="@id/AddButton"
app:layout_constraintTop_toBottomOf="@+id/CustomerAge" />
<Switch
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
android:id="@+id/StatusSwitch"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:backgroundTint="#E91E63"
android:text="Active Status"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ViewAll" />
<ListView
android:id="@+id/ListSection"
style="@android:style/Widget.DeviceDefault.Light.ExpandableListView"
android:layout_width="409dp"
android:layout_height="500dp"
app:circularflow_radiusInDP="12dp"
android:layout_marginStart="16dp"
android:animateLayoutChanges="true"
android:animationCache="true"
android:background="#AD03A9F4"
android:fontFamily="sans-serif-medium"
android:padding="12dp"
android:paddingBottom="12dp"
android:textAlignment="gravity"
android:textColor="#FF5722"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/StatusSwitch"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/DeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:autoText="true"
android:backgroundTint="#03A9F4"
android:onClick="Deletedatabase"
android:text="Delete "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.757"
app:layout_constraintStart_toEndOf="@+id/ViewAll"
app:layout_constraintTop_toBottomOf="@+id/CustomerAge" />
<EditText
android:id="@+id/CustomerName"
android:layout_width="0dp"
AAD BS(CS)-6-a&b SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="55dp"
android:layout_marginEnd="24dp"
android:background="@drawable/rounded_edittext"
android:ems="10"
android:gravity="center_vertical"
android:hint="Customer Name"
android:inputType="text"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.477"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AAD BS(CS)-6-a&b SZABIST-ISB