[英]Pass FirebaseFirestore reference in Intents
I need to send a FirebaseFirestore reference of a collection in intents from one activity to another which will be used to add documents by the second activity.我需要将一个集合的 FirebaseFirestore 引用从一个活动发送到另一个活动,第二个活动将用于添加文档。 How can I do that?我怎样才能做到这一点?
Pass the path of the document as a string as an extra in the intent. 将文档的路径作为字符串传递,作为附加的意图。 You can use getPath() to get the path from a DocumentReference, then use that same string to build a new DocumentReference by passing it to the document() method of FirebaseFirestore. 您可以使用getPath()从DocumentReference获取路径,然后使用相同的字符串通过将其传递到FirebaseFirestore的document()方法来构建新的DocumentReference。
Try using getPath() and then passing it to document reference.尝试使用 getPath() 然后将其传递给文档引用。
Example:例子:
Getting path from the reference从参考中获取路径
String documentId = getSnapshots().getSnapshot(position).getReference().getPath();
// Bring users to View Event when clicking on viewEventButton
holder.viewEventButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view1) {
Intent intent = new Intent(getActivity(), ViewEventActivity.class);
intent.putExtra("documentId",documentId);
((MainPageActivity) getActivity()).startActivity(intent);
}
});
Getting Document from String path从字符串路径获取文档
String documentId = getIntent().getStringExtra("documentId");
String documentName = documentId.substring(documentId.lastIndexOf("/") + 1);
// TODO how to reflect document path based on the event that users click on the app -> documentPath -> SharedPreferences from HomePage
DocumentReference docRef = db.collection("Events").document(documentName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.