Created
September 6, 2023 09:45
-
-
Save amoozeshbebin/856a732977f57cfcc4c20d872aed33a3 to your computer and use it in GitHub Desktop.
UserManagement Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
activity_main.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView 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" | |
android:padding="30dp" | |
tools:context=".MainActivity"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/userManagement" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:fontFamily="casual" | |
android:text="@string/usermanagement" | |
android:textSize="25sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<EditText | |
android:id="@+id/usernametxt" | |
android:layout_width="358dp" | |
android:layout_height="46dp" | |
android:layout_marginBottom="20dp" | |
android:autofillHints="" | |
android:ems="10" | |
android:hint="@string/username" | |
android:inputType="text" | |
app:layout_constraintBottom_toTopOf="@+id/firstnametxt" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
tools:ignore="VisualLintTextFieldSize" /> | |
<EditText | |
android:id="@+id/firstnametxt" | |
android:layout_width="352dp" | |
android:layout_height="41dp" | |
android:layout_marginBottom="20dp" | |
android:autofillHints="" | |
android:ems="10" | |
android:hint="@string/firsname" | |
android:inputType="text" | |
app:layout_constraintBottom_toTopOf="@+id/lastnametxt" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.272" | |
app:layout_constraintStart_toStartOf="parent" | |
tools:ignore="VisualLintTextFieldSize" /> | |
<EditText | |
android:id="@+id/lastnametxt" | |
android:layout_width="348dp" | |
android:layout_height="41dp" | |
android:layout_marginTop="164dp" | |
android:autofillHints="" | |
android:ems="10" | |
android:hint="@string/lastname" | |
android:inputType="text" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/userManagement" | |
tools:ignore="VisualLintTextFieldSize" /> | |
<soup.neumorphism.NeumorphButton | |
android:id="@+id/btnadduser" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="32dp" | |
android:layout_marginBottom="296dp" | |
android:contentDescription="@string/app_name" | |
android:padding="28dp" | |
android:src="@drawable/baseline_add_24" | |
android:text="@string/add_user" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:neumorph_backgroundColor="@color/background" | |
app:neumorph_shadowColorDark="@color/dark_shadow" | |
app:neumorph_shadowColorLight="@color/light_shadow" | |
app:neumorph_shadowElevation="5dp" /> | |
<soup.neumorphism.NeumorphButton | |
android:id="@+id/btndeleteuser" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="16dp" | |
android:layout_marginBottom="196dp" | |
android:contentDescription="@string/app_name" | |
android:padding="28dp" | |
android:src="@drawable/baseline_add_24" | |
android:text="@string/delete_user" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/listview" | |
app:layout_constraintTop_toBottomOf="@+id/btnadduser" | |
app:neumorph_backgroundColor="@color/background" | |
app:neumorph_shadowColorDark="@color/dark_shadow" | |
app:neumorph_shadowColorLight="@color/light_shadow" | |
app:neumorph_shadowElevation="5dp" /> | |
<ListView | |
android:id="@+id/listview" | |
android:layout_width="179dp" | |
android:layout_height="410dp" | |
android:layout_marginTop="16dp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.023" | |
app:layout_constraintStart_toStartOf="parent" | |
android:tooltipText="fdfsf" | |
app:layout_constraintTop_toBottomOf="@+id/lastnametxt" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</ScrollView> | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
MainActivity.kt: | |
package com.example.sql | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.ArrayAdapter | |
import android.widget.Button | |
import android.widget.EditText | |
import android.widget.ListView | |
import android.widget.Toast | |
import soup.neumorphism.NeumorphFloatingActionButton | |
class MainActivity : AppCompatActivity() { | |
private lateinit var username: EditText | |
private lateinit var firstName: EditText | |
private lateinit var lastName: EditText | |
private lateinit var btnAdd: Button | |
private lateinit var btnDelete: Button | |
private lateinit var listView: ListView | |
private lateinit var userDBHelper: UserDBHelper | |
private lateinit var adapter: ArrayAdapter<String> | |
private var allUsers = ArrayList<user>() | |
private var listItems = ArrayList<String>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
init() | |
} | |
private fun init() { | |
bindViews() | |
userDBHelper = UserDBHelper(this) | |
refreshList() | |
adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems) | |
listView.adapter = adapter | |
btnAdd.setOnClickListener { | |
if (username.text.isEmpty()) { | |
Toast.makeText(this, "Please Enter Username", Toast.LENGTH_SHORT).show() | |
} | |
if (firstName.text.isEmpty()) { | |
Toast.makeText(this, "Please Enter Username", Toast.LENGTH_SHORT).show() | |
} | |
if (lastName.text.isEmpty()) { | |
Toast.makeText(this, "Please Enter Username", Toast.LENGTH_SHORT).show() | |
} | |
val User = | |
user(username.text.toString(), firstName.text.toString(), lastName.text.toString()) | |
val result = userDBHelper.insertUser(User) | |
if (result) { | |
Toast.makeText(this, "New User Added Successfully", Toast.LENGTH_SHORT).show() | |
username.text.clear() | |
firstName.text.clear() | |
lastName.text.clear() | |
refreshList() | |
adapter.notifyDataSetChanged() | |
} | |
} | |
listView.setOnItemClickListener { parent, view, position, id -> | |
val item = allUsers[position] | |
username.setText(item.userName) | |
firstName.setText(item.firstName) | |
lastName.setText(item.lastName) | |
} | |
btnDelete.setOnClickListener{ | |
if (username.text.isEmpty()) { | |
Toast.makeText(this, "Please Enter Username", Toast.LENGTH_SHORT).show() | |
} | |
val result = userDBHelper.deleteuserbyusername(username.text.toString()) | |
if (result) { | |
Toast.makeText(this, "User deleted Successfully", Toast.LENGTH_SHORT).show() | |
username.text.clear() | |
firstName.text.clear() | |
lastName.text.clear() | |
refreshList() | |
adapter.notifyDataSetChanged() | |
}else{ | |
Toast.makeText(this, "User Not Found!", Toast.LENGTH_SHORT).show() | |
} | |
} | |
} | |
private fun refreshList() { | |
allUsers.clear() | |
allUsers.addAll(userDBHelper.getAllUsers()) | |
listItems.clear() | |
listItems.addAll( | |
allUsers.map { x -> "${x.firstName} ${x.lastName} (${x.userName})" } as ArrayList<String> | |
) | |
} | |
private fun bindViews() { | |
username = findViewById(R.id.usernametxt) | |
firstName = findViewById(R.id.firstnametxt) | |
lastName = findViewById(R.id.lastnametxt) | |
btnAdd = findViewById(R.id.btnadduser) | |
btnDelete = findViewById(R.id.btndeleteuser) | |
listView = findViewById(R.id.listview) | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
user.kt: | |
package com.example.sql | |
data class user(val userName:String,val firstName:String,val lastName:String) | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
dbcontract.object: | |
package com.example.sql | |
import android.provider.BaseColumns | |
object DBContract { | |
class userEntity :BaseColumns{ | |
companion object{ | |
val TABLE_NAME = "users" | |
val COLUMN_USERNAME = "username" | |
val COLUMN_FIRSNAME = "firstname" | |
val COLUMN_LASTNAME = "lastname" | |
} | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ | |
UserDBHelper.kt: | |
package com.example.sql | |
import android.annotation.SuppressLint | |
import android.content.ContentValues | |
import android.content.Context | |
import android.database.Cursor | |
import android.database.sqlite.SQLiteDatabase | |
import android.database.sqlite.SQLiteOpenHelper | |
import androidx.appcompat.app.AlertDialog | |
import java.lang.Exception | |
import soup.neumorphism.NeumorphFloatingActionButton | |
class UserDBHelper(context: Context) : | |
SQLiteOpenHelper(context, database_name, null, database_version) { | |
override fun onCreate(db: SQLiteDatabase?) { | |
db?.execSQL(SQL_CREATE_QUERY) | |
} | |
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | |
db?.execSQL(SQL_DELETE_QUERY) | |
onCreate(db) | |
} | |
fun insertUser(user: user): Boolean { | |
return try { | |
val db = writableDatabase | |
val valuse = ContentValues() | |
valuse.put(DBContract.userEntity.COLUMN_USERNAME, user.userName) | |
valuse.put(DBContract.userEntity.COLUMN_FIRSNAME, user.firstName) | |
valuse.put(DBContract.userEntity.COLUMN_LASTNAME, user.lastName) | |
db.insert(DBContract.userEntity.TABLE_NAME, null, valuse) | |
true | |
} catch (e: Exception) { | |
false | |
} | |
} | |
@SuppressLint("Range") | |
fun getAllUsers(): ArrayList<user> { | |
val users = ArrayList<user>() | |
val db = readableDatabase | |
var cursor: Cursor? = null | |
try { | |
cursor = db.rawQuery("SELECT * FROM ${DBContract.userEntity.TABLE_NAME}", null) | |
} catch (e: Exception) { | |
db.execSQL(SQL_CREATE_QUERY) | |
return ArrayList() | |
} | |
var username: String | |
var firstname: String | |
var lastname: String | |
if (cursor.moveToFirst()) { | |
while (!cursor.isAfterLast) { | |
username = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_USERNAME)) | |
firstname = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_FIRSNAME)) | |
lastname = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_LASTNAME)) | |
users.add(user(username, firstname, lastname)) | |
cursor.moveToNext() | |
} | |
} | |
return users | |
} | |
@SuppressLint("Range") | |
fun getUserByUsername(username: String): user? { | |
val db = readableDatabase | |
var cursor: Cursor? = null | |
try { | |
cursor = db.rawQuery( | |
"SELECT * FROM ${DBContract.userEntity.TABLE_NAME} where ${DBContract.userEntity.COLUMN_USERNAME} = ?", | |
arrayOf(username) | |
) | |
} catch (e: Exception) { | |
db.execSQL(SQL_CREATE_QUERY) | |
return null | |
} | |
var username: String = "" | |
var firstname: String = "" | |
var lastname: String = "" | |
if (cursor.moveToFirst()) { | |
while (!cursor.isAfterLast) { | |
username = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_USERNAME)) | |
firstname = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_FIRSNAME)) | |
lastname = | |
cursor.getString(cursor.getColumnIndex(DBContract.userEntity.COLUMN_LASTNAME)) | |
cursor.moveToNext() | |
} | |
} else { | |
return null | |
} | |
return user(username, firstname, lastname) | |
} | |
fun deleteuserbyusername(username: String): Boolean { | |
return try { | |
val user = getUserByUsername(username) ?: throw Exception("User Not Found!") | |
val db = writableDatabase | |
val where = "${DBContract.userEntity.COLUMN_USERNAME} = ?" | |
val args = arrayOf(username) | |
db.delete(DBContract.userEntity.TABLE_NAME, where, args) | |
true | |
} catch (e: Exception) { | |
false | |
} | |
} | |
companion object { | |
const val database_version = 1 | |
const val database_name = "usermanagement.db" | |
private val SQL_CREATE_QUERY = "CREATE TABLE ${DBContract.userEntity.TABLE_NAME}" + | |
" (${DBContract.userEntity.COLUMN_USERNAME} TEXT PRIMARY KEY," + | |
" ${DBContract.userEntity.COLUMN_FIRSNAME} TEXT ," + | |
" ${DBContract.userEntity.COLUMN_LASTNAME} TEXT )" | |
private val SQL_DELETE_QUERY = "DROP TABLE IF EXISTS ${DBContract.userEntity.TABLE_NAME}" | |
} | |
} | |
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment