Creating entity

Let's create a class of user named UserModel.kt with the @Entity annotation so that each user is an entity. All variables columns shouldn't be private and so that Room will able to instantiate your objects:

@Entity(tableName = "users")
class Users(): Parcelable {
@PrimaryKey(autoGenerate = true)
@NonNull
@ColumnInfo(name = "userId")
var userId: Int = 0

@NonNull
@ColumnInfo(name = "username")
lateinit var username: String

@NonNull
@ColumnInfo(name = "email")
lateinit var email: String

@NonNull
@ColumnInfo(name = "contactNumber")
lateinit var contactNumber: String

@NonNull
@ColumnInfo(name = "address")
lateinit var address: String

constructor(username: String, email: String, contactNumber: String, address: String):this(){
this.username = username
this.email = email
this.contactNumber = contactNumber
this.address = address
}

override fun toString(): String {
return "Users(username='$username', email='$email', contactNumber='$contactNumber', address='$address')"
}
}

Let's see what is in this class:

  • @Entity(tableName = "users"): An entity class represents a table, and our table name is users
  • @ColumnInfo(name = "**"): This specifies a name in the table
  • @PrimaryKey(autoGenerate = true): This means the ID is our primary key and it will automatically increase the value
  • @NonNull: This means there will be no null or empty value in the columns

To pass this object from one activity to another, we need to convert this class into a Parcelable class. So let's extend this class. In the traditional way, it will need lots of code like the following:

@Entity(tableName = "users")
class Users(): Parcelable {
----
----
constructor(parcel: Parcel) : this() {
userId = parcel.readInt()
username = parcel.readString()!!
email = parcel.readString()!!
contactNumber = parcel.readString()!!
address = parcel.readString()!!
}
----
----
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(userId)
parcel.writeString(username)
parcel.writeString(email)
parcel.writeString(contactNumber)
parcel.writeString(address)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<Users> {
override fun createFromParcel(parcel: Parcel): Users {
return Users(parcel)
}

override fun newArray(size: Int): Array<Users?> {
return arrayOfNulls(size)
}
}
}

So, it's really complex to understand and handle, though we don't need to modify the override functions and constructors. However, if you omit these lines, then, of course, you will be happy, and your code will look nice. To do this, we need to apply the lazy coder's way.

We just need an annotation named @Parcelize on top of the model class. Here is the full code for this:

@Parcelize
@Entity(tableName = "users")
class Users(): Parcelable {
@PrimaryKey(autoGenerate = true)
@NonNull
@ColumnInfo(name = "userId")
var userId: Int = 0

@NonNull
@ColumnInfo(name = "username")
lateinit var username: String

@NonNull
@ColumnInfo(name = "email")
lateinit var email: String

@NonNull
@ColumnInfo(name = "contactNumber")
lateinit var contactNumber: String

@NonNull
@ColumnInfo(name = "address")
lateinit var address: String

constructor(username: String, email: String, contactNumber: String, address: String):this(){
this.username = username
this.email = email
this.contactNumber = contactNumber
this.address = address
}

override fun toString(): String {
return "Users(username='$username', email='$email', contactNumber='$contactNumber', address='$address')"
}
}

So there is no more extra code. To enable this, you need to add the following code in the android block of the build.gradle (Module: app) file:

android {
----
----
androidExtensions {
experimental = true
}
}
dependencies {
----
----
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.177.39