【问题标题】:How do I get the id of my newly created document in android studio?如何在 android studio 中获取我新创建的文档的 id?
【发布时间】:2021-08-18 12:28:29
【问题描述】:

一直试图从我的新产品哈希映射中获取 id 以发送到另一个活动,但从 logD 我只获取集合名称,例如。产品而不是 eg.Jfchd1jff3k

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 的值?
  • 是的,我在其他活动中使用了 intent.getStringExtra("ProductID") 来获取值,但来自我的 logD,而不是我获取作为集合名称的“Products”的 id

标签: java android firebase google-cloud-firestore


【解决方案1】:

要获取您创建的文档的 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。

【讨论】:

    【解决方案2】:

    是的,因为您试图从没有 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

    String id= productDocumentRef.getId();
    

    【讨论】:

      猜你喜欢
      • 2019-02-19
      • 2021-11-01
      • 2020-07-18
      • 1970-01-01
      • 2019-10-06
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多