How to create an MVVM entity class in Kotlin

Andros Adrianopolos :

I have an entity class for an app I'm building in Java that looks like this:

@Entity(tableName = "entry_table")
public class Entry {

    @PrimaryKey(autoGenerate = true)
    private int id;

    private String username, hint, password;

    public Entry(String username, String hint, String password){
        this.username = username;
        this.hint = hint;
        this.password = password;
    }

    public Entry(){}

    public int getId() {return id;}

    public void setId(int id) {this.id = id;}

    public String getUsername() {return username;}

    public void setUsername(String username) {this.username = username;}

    public String getHint() {return hint;}

    public void setHint(String hint) {this.hint = hint;}

    public String getPassword() {return password;}

    public void setPassword(String password) {this.password = password;}
}

I'm trying to do the same thing in Kotlin. I thought about converting the file to Kotlin but I want to get my hands used to writing Kotlin code. Some of the implementations that I found online were quick to throw errors.

This is what I have so far:

@Entity(tableName = "entry_table")

data class Entry()
Saurabh Thorat :

Your entity in Kotlin would look like this:

@Entity(tableName = "entry_table")
data class Entry(@PrimaryKey(autoGenerate = true) val id: Int,
                 val username: String,
                 val hint: String,
                 val password: String)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148925&siteId=1