一直试图从我的新产品哈希图中获取 id 以发送到另一个活动,但从 logD 我一直只获取集合名称,例如。产品而不是 eg.Jfchd1jff3k<- 从 Firestore 随机生成的 id 有没有办法在创建后立即获得它?
enter code here
公共类 newProduct 扩展 AppCompatActivity {
private Button addProduct;
private EditText productName, productDescription, productPrice;
private Spinner category;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
FirebaseUser fUser;
StorageReference storageReference;
private String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_product);
fAuth = FirebaseAuth.getInstance();
fUser = fAuth.getCurrentUser();
fStore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
addProduct = findViewById(R.id.btn_addProduct);
productName = findViewById(R.id.et_productName);
productDescription = findViewById(R.id.et_productDescription);
productPrice = findViewById(R.id.et_productPrice);
category = findViewById(R.id.catSpinner);
String[] arraySpinner = new String[]{
"Yoga", "Lifting", "Water Sports", "Pilates", "Other"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapter);
addProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveProduct();
}
});
}
private void saveProduct(){
userID = fAuth.getCurrentUser().getUid();
DocumentReference df = fStore.collection("Users").document(userID);
DocumentReference df2 = fStore.collection("Instructors").document(userID);
String name = productName.getText().toString();
String description = productDescription.getText().toString();
String price = productPrice.getText().toString();
String cat = category.getSelectedItem().toString();
if(name.trim().isEmpty() || description.trim().isEmpty() || price.trim().isEmpty()){
Toast.makeText(this,"Fields Cannot Be Empty", Toast.LENGTH_SHORT).show();
return;
}
df.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.getString("isuser") != null){
String userContact = documentSnapshot.getString("Mobile");
CollectionReference productsRef = FirebaseFirestore.getInstance()
.collection("Products");
Map<String, Object> product = new HashMap<>();
product.put("userID", userID);
product.put("productName", name);
product.put("productDescription", description);
product.put("price", price);
product.put("category", cat);
product.put("contact", userContact);
productsRef.add(product);
//this is the part where i try to get the id
String id= productsRef.getId();
Intent intent = new Intent(getApplicationContext(), saveProductImage.class);
intent.putExtra("productID", id);
startActivity(intent);
finish();
}else{
df2.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.getString("isinstructor") !=null){
String userContact = documentSnapshot.getString("Mobile");
CollectionReference productsRef = FirebaseFirestore.getInstance()
.collection("Products");
Map<String, Object> product = new HashMap<>();
product.put("userID", userID);
product.put("productName", name);
product.put("productDescription", description);
product.put("price", price);
product.put("category", cat);
product.put("contact", userContact);
productsRef.add(product);
finish();
}
}
});
}
}
});
}
}
要获取您创建的文档的 ID,请更改以下代码行:
String id= productsRef.getId();
到:
String id= productsRef.document().getId();
将其添加到您的intent
对象中:
intent.putExtra("productID", id);
然后在第二个活动中读回它,如下所示:
String id = intent.getStringExtra("ProductID");
Lod.d("TAG", id);
它应该在控制台中打印在 Firestore 中添加的最后一个文档的 ID。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句