[英]How Can I get Unique UserID generated by FireStore using Android?
[英]How to autoincrement userID in Firestore?
所以我創建了一個預訂應用程序,它將數據發送到預訂集合中,以用戶 ID 作為文檔名稱。 但是當用戶再次預訂時,它會覆蓋之前的預訂用戶 ID 文檔。 我想要做的是: 1)在 userID 上實現自動增量,以及2)也顯示自動增量預訂(注意我正在使用 userID,因為我想讓它只對那個用戶唯一)。 3)如果有人可以提供幫助,我還想對預訂實施限制。
將數據發送到 BOOKING COLLECTION 並使用 userID 創建 DOCUMENT 的代碼:
public class bookingpage extends AppCompatActivity {
EditText mFirstnamelastname,mMobnum,mPincode,mFlatno,mArea,mLandmark,mTown,mState;
Button mBook;
String userID;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookingpage);
//pickup
mFirstnamelastname=findViewById(R.id.firstlastname);
mMobnum=findViewById(R.id.mobnum);
mPincode=findViewById(R.id.pincode);
mFlatno=findViewById(R.id.flatno);
mArea=findViewById(R.id.area);
mLandmark=findViewById(R.id.landmark);
mTown=findViewById(R.id.town);
mState=findViewById(R.id.state);
mBook=findViewById(R.id.editbook);
progressBar=findViewById(R.id.progressBar4);
fAuth=FirebaseAuth.getInstance();
fstore=FirebaseFirestore.getInstance();
mBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//pickup
String firstname = mFirstnamelastname.getText().toString().trim();
String phoneno = mMobnum.getText().toString().trim();
String pincode = mPincode.getText().toString().trim();
String flatno = mFlatno.getText().toString().trim();
String area = mArea.getText().toString().trim();
String landmark = mLandmark.getText().toString().trim();
String town = mTown.getText().toString().trim();
String state = mState.getText().toString().trim();
progressBar.setVisibility(View.VISIBLE);
//saving data
userID=fAuth.getCurrentUser().getUid();
//creating a document reference creating a collection booking and making a new doc using user id
DocumentReference documentReference = fstore.collection("Booking").document(userID);
//creating a hashmap to send data
Map<String,Object> book = new HashMap<>();
//setting status
book.put("Status -","Active");
//pickup
book.put("a1 - Fullname",firstname);
book.put("a2 - PhoneNo",phoneno);
book.put("a3 - Pincode",pincode);
book.put("a4 - Flatno",flatno);
book.put("a5 - Area",area);
book.put("a6 - Landmark",landmark);
book.put("a7 - Town",town);
book.put("a8 - State",state);
//using the document reference to set user document
documentReference.set(book).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(bookingpage.this, "Booking Successful", Toast.LENGTH_SHORT).show();
Log.d("Tag","onSuccess: Successfully booked for "+ userID);
startActivity(new Intent(getApplicationContext(),MainActivity2.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(bookingpage.this, "Error!" + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
顯示帶有 userID 的預訂集合文檔的代碼:
final DocumentReference documentReference = fstore.collection("Booking").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
//putting if else fixed crashing
if (e != null) {
Log.d("Tag", "Error:" + e.getMessage());
} else {
mStatus.setText(documentSnapshot.getString("Status -"));
mFirstnamelastname.setText(documentSnapshot.getString("a1 - Fullname"));
mMobnum.setText(documentSnapshot.getString("a2 - PhoneNo"));
mPincode.setText(documentSnapshot.getString("a3 - Pincode"));
mFlatno.setText(documentSnapshot.getString("a4 - Flatno"));
mArea.setText(documentSnapshot.getString("a5 - Area"));
mLandmark.setText(documentSnapshot.getString("a6 - Landmark"));
mTown.setText(documentSnapshot.getString("a7 - Town"));
mState.setText(documentSnapshot.getString("a8 - State"));
}
}
});
我是初學者,請嘗試解釋您的答案,以便我可以理解和了解更多信息:)
嗯...您只是不應該在 Booking 集合中使用 UserId 作為文檔名稱。 這只是違反邏輯和最佳實踐。
您應該改為讓 Firestore 創建一個BookingId 。 這將是您當前的預訂文檔 + 一個新字段(字符串),其中包含進行預訂的用戶的 UserId。
這將是一種更合乎邏輯(和可擴展)的方式。
要限制預訂數量,您可以在 UserId 文檔(在 users 集合中)中添加一個字段,稱為 bookingCount (Integer)。 每次用戶預訂時,檢查 bookingCount >= bookingLimit(您選擇的任意值)。
如果 bookingCount < bookingLimit,則允許他們預訂並將bookingCount增加 1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.