【发布时间】: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