-
Notifications
You must be signed in to change notification settings - Fork 11
Implemented rooms db to store person's information and implemented insert function for db #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.neilkrishna.basicapplication | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import android.view.View | ||
| import android.widget.Toast | ||
| import androidx.lifecycle.Observer | ||
| import androidx.room.Room | ||
| import com.neilkrishna.basicapplication.databinding.ActivityAddPeopleInfoBinding | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.GlobalScope | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class AddPeopleInfoActivity : AppCompatActivity() { | ||
| lateinit var database: PersonDatabase | ||
| private lateinit var binding: ActivityAddPeopleInfoBinding | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityAddPeopleInfoBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
| database=PersonDatabase.getDatabase(this) | ||
| binding.btnAddPersonInfo.setOnClickListener{ | ||
|
|
||
| addPerson()} | ||
|
|
||
| } | ||
|
|
||
| private fun addPerson() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put in error messages if any fields are missing. |
||
| val firstName = binding.etFirstName.text.toString() | ||
| val lastName = binding.etLastName.text.toString() | ||
| val age = binding.etAge.text.toString() | ||
| val gender = binding.etGender.text.toString() | ||
| val email = binding.etEmail.text.toString() | ||
|
|
||
| if(firstName.isNotEmpty() && lastName.isNotEmpty() && age.isNotEmpty() && gender.isNotEmpty() && email.isNotEmpty() ) { | ||
| val person = Person(0, firstName, lastName, age.toInt(), gender, email) | ||
|
|
||
| GlobalScope.launch(Dispatchers.IO) | ||
| { | ||
| database.personDao().insertPerson(person) | ||
| } | ||
|
|
||
| binding.etAge.text.clear() | ||
| binding.etGender.text.clear() | ||
| binding.etEmail.text.clear() | ||
| binding.etFirstName.text.clear() | ||
| binding.etLastName.text.clear() | ||
| Toast.makeText(this@AddPeopleInfoActivity,"Successfully written",Toast.LENGTH_SHORT).show() | ||
| }else Toast.makeText(this@AddPeopleInfoActivity,"PLease Enter Data",Toast.LENGTH_SHORT).show() | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.neilkrishna.basicapplication | ||
|
|
||
| import androidx.room.Entity | ||
| import androidx.room.PrimaryKey | ||
|
|
||
| @Entity(tableName = "person") | ||
| data class Person( | ||
| @PrimaryKey(autoGenerate = true) | ||
| val id : Long, | ||
| val firstname : String, | ||
| val lastname : String, | ||
| val age : Int, | ||
| val gender : String, | ||
| val email : String | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.neilkrishna.basicapplication | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.room.Dao | ||
| import androidx.room.Insert | ||
| import androidx.room.Query | ||
|
|
||
| @Dao | ||
| interface PersonDAO { | ||
| @Insert | ||
| fun insertPerson(person : Person) | ||
|
|
||
| @Query("SELECT * FROM person") | ||
| fun getPerson() : LiveData<List<Person>> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here u can use Flow and get it as Live data in your View model |
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.neilkrishna.basicapplication | ||
|
|
||
| import android.content.Context | ||
| import androidx.room.Database | ||
| import androidx.room.Room | ||
| import androidx.room.RoomDatabase | ||
|
|
||
| @Database(entities = [Person::class], version = 1) | ||
|
|
||
| abstract class PersonDatabase : RoomDatabase() { | ||
|
|
||
| abstract fun personDao() : PersonDAO | ||
|
|
||
| companion object{ | ||
|
|
||
| @Volatile | ||
| private var INSTANCE : PersonDatabase? = null | ||
|
|
||
| fun getDatabase(context:Context):PersonDatabase{ | ||
| val tempInstance = INSTANCE | ||
| if(tempInstance != null){ | ||
| return tempInstance | ||
| } | ||
| synchronized(this){ | ||
| val instance = Room.databaseBuilder( | ||
| context.applicationContext, | ||
| PersonDatabase::class.java, | ||
| "PersonINFO" | ||
| ).build() | ||
| INSTANCE = instance | ||
| return instance | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.appcompat.widget.LinearLayoutCompat 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=".AddPeopleInfoActivity" | ||
| android:orientation="vertical" | ||
| android:layout_gravity="center" | ||
| android:gravity="center" | ||
| android:layout_margin="30dp"> | ||
|
|
||
|
|
||
|
|
||
| <TextView | ||
| android:id="@+id/textView" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="50dp" | ||
| android:fontFamily="serif" | ||
| android:text="Person's Data" | ||
| android:textColor="@color/white" | ||
| android:textSize="30dp" | ||
| android:layout_marginBottom="30dp" | ||
| app:layout_constraintLeft_toLeftOf="parent" | ||
| app:layout_constraintRight_toRightOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" /> | ||
|
|
||
| <EditText | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refer to Material 3 implementation of Edit text. also put in input type for the type of input ur expecting. |
||
| android:id="@+id/et_first_name" | ||
| android:hint= " First Name" | ||
| android:layout_marginBottom="16dp" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| /> | ||
| <EditText | ||
| android:id="@+id/et_last_name" | ||
| android:hint= " Last Name" | ||
| android:layout_marginBottom="16dp" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
|
|
||
| /> | ||
| <EditText | ||
| android:id="@+id/et_age" | ||
| android:hint= " Age" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginBottom="16dp" | ||
| /> | ||
| <EditText | ||
| android:id="@+id/et_gender" | ||
| android:hint= " Gender" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginBottom="16dp" | ||
| /> | ||
| <EditText | ||
| android:id="@+id/et_email" | ||
| android:hint= " Email" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginBottom="16dp" | ||
| /> | ||
|
|
||
| <Button | ||
| android:id="@+id/btn_add_person_info" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="16dp" | ||
| android:text = "Add Person Info" | ||
| /> | ||
|
|
||
| </androidx.appcompat.widget.LinearLayoutCompat> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plz implement the view model for insertion into the database and make use of View model coroutine scope instead of global scope. Also include a circular progress bar which will be displayed while the entry is inserted into the database.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you elaborate about circular progress bar? i didn't get it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
U display the progress bar before u start the transaction to database and hide it as u complete the transaction