简体   繁体   English

Kotlin:如何使用 Kotlin Room 生成唯一 id

[英]Kotlin : How to use Kotlin Room to generate a unique id

in my application,apart from the primarykey of a user,I will still need to generate a unique ID(12 -15 charactors) for showing to the public.在我的应用程序中,除了用户的主键外,我仍然需要生成一个唯一的 ID(12 -15 个字符)以向公众展示。

I am new with Room,Could you please take a look my code?我是Room的新手,你能看看我的代码吗?

Thank you so much in advance !非常感谢您!

@Entity
data class UserInfo(
    @PrimaryKey(autoGenerate = true)
    val userId:Int = 0,
    val image: String,
    val imageSource : String,//Local or Online
    val username : String,
    val gender :String,
    val userPublicId:String, //this is the id that need to generate 12-15 
    val birthday : String,
    val userIntro :String,
   
)

As mentioned @Tenfour04 you can use some ways to generate random string.正如@Tenfour04 提到的,您可以使用一些方法来生成随机字符串。 I think org.apache.commons.lang3.RandomStringUtils is a good choice.我认为org.apache.commons.lang3.RandomStringUtils是一个不错的选择。 For exmaple look at the following methods: randomAscii , randomAlphabetic , randomAlphanumeric .例如查看以下方法: randomAsciirandomAlphabeticrandomAlphanumeric

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM