How to auto increment unique number in android studio using firebase real-time database Like unique id : HDTICKET00001, HDTICKET00002 HDTICKET00003
Now changed unique id format to eg.HDTICKET5000001,HDTICKET5000002
This code working with system emulator perfectly
if used physical device as emulator first ticket generated correctly after app automatically closed
How to solve the same..
firebaseDatabase = FirebaseDatabase.getInstance(); databaseReference = firebaseDatabase.getReference(); Query query = databaseReference.child("TICKET_DETAIL").orderByChild("TICKET_NO").limitToLast(1); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
if (snapshot.getValue()!=null){
String reslt = snapshot.getValue().toString();
String [] role = reslt.split(",");
String tiketno = role[4];
String [] ref = tiketno.split("HDTICKET",2);
int number = Integer.parseInt(ref[1])+1;
final String Tktno = "HDTICKET"+number;
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("TICKET_NO",Tktno);
hashMap.put("TICKET_DATE",ticketdt);
hashMap.put("UID",usrid);
hashMap.put("MESSAGE",msg);
hashMap.put("TICKET_STATUS",ticktstatus);
hashMap.put("DONE_BY",doneby);
hashMap.put("CLOSED_DATE",donedate);
databaseReference.child("TICKET_DETAIL")
.child(Tktno)
.setValue(hashMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
//Toast.makeText(RaiseTicket.this, "Ticket Generated successfully", Toast.LENGTH_SHORT).show();
RaiseTicket.this.finish();
Intent sucess = new Intent(RaiseTicket.this, RaiseTicketsuccess.class);
String user = Tktno;
sucess.putExtra("TICKET#",user);
startActivity(sucess);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull @NotNull Exception e) {
Toast.makeText(RaiseTicket.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
final String Tktno = "HDTICKET5000001";
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("TICKET_NO",Tktno);
hashMap.put("TICKET_DATE",ticketdt);
hashMap.put("UID",usrid);
hashMap.put("MESSAGE",msg);
hashMap.put("TICKET_STATUS",ticktstatus);
hashMap.put("DONE_BY",doneby);
hashMap.put("CLOSED_DATE",donedate);
databaseReference.child("TICKET_DETAIL")
.child(Tktno)
.setValue(hashMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
//Toast.makeText(RaiseTicket.this, "Ticket Generated successfully", Toast.LENGTH_SHORT).show();
RaiseTicket.this.finish();
Intent sucess = new Intent(RaiseTicket.this, RaiseTicketsuccess.class);
String user = Tktno;
sucess.putExtra("TICKET#",user);
startActivity(sucess);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull @NotNull Exception e) {
Toast.makeText(RaiseTicket.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
}
});