I just create unique id but with random number. but I want to create that id like jason-001,smith-002,etc.
Integer user_id= new Random().nextInt();
//implementation
reference_uploaded_by.getRef().setValue(name.getText().toString() + "-" + user_id);
You can use AtomicLong#incrementAndGet
as a counter. The “Atomic” means the class is thread-safe .
AtomicLong userIdIncrementor = new AtomicLong(previouslyUsedNumber);
Use that object to get incrementing numbers for each new user.
long number = userIdIncrementor.incrementAndGet();
String userId = userName + String.format("%03d", number);
This code assumes you will never have more than a thousand users, with only three digits for the number as you specified in your Question.
For details on formatting an integer, see this Answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.