我如何得到我在android工作室新创建的文档的id ?



一直在尝试从我的新产品哈希图中获取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。

是的,因为您正试图从没有ID的集合中获取它。文档本身有一个id,可以通过简单地调用ref.document()来创建,并返回带有其id的文档。

试试这个

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);
DocumentReference productDocumentRef = productsRef.document();
productDocumentRef.set(product);
然后得到id by

String id= productDocumentRef.getId();

最新更新