![](/img/trans.png)
[英]i want to get the user unique id. how do i fetch it using getKey method
[英]I want to create unique user id
我只是创建唯一的ID,但使用随机数。 但我想创建该ID,例如jason-001,smith-002等。
Integer user_id= new Random().nextInt();
//implementation
reference_uploaded_by.getRef().setValue(name.getText().toString() + "-" + user_id);
您可以将AtomicLong#incrementAndGet
用作计数器。 “原子”表示该类是线程安全的 。
AtomicLong userIdIncrementor = new AtomicLong(previouslyUsedNumber);
使用该对象获取每个新用户的递增编号。
long number = userIdIncrementor.incrementAndGet();
String userId = userName + String.format("%03d", number);
此代码假定您永远不会有超过一千个用户,并且您在“问题”中指定的号码只有三位数字。
有关格式化整数的详细信息,请参见此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.