将firebase数据库url(String)分配给Database Reference变量? [英] Assign a firebase database url(String) to a Database Reference variable?
问题描述
我试图将包含数据库中某个位置的url的字符串分配给DatabaseReference类型. 示例:
I'm trying to assign a string that contains a url for a place in my database to a DatabaseReference type. Example:
String strRef = "my database url";
DatabaseReference ref = //here I want to assign the content of strRef.
更新(从评论到Alex的回答):
Update (from comment to Alex's answer):
我从另一个活动中获得了指向数据库中某个位置的特定URL.现在,我需要将其转换为DatabaseReference类型,以便能够从数据库中的该位置读取该URL.
I'm getting from another activity a specific url that point on a place in my database.and now I need to cast it to a DatabaseReference type that I'll be able to read from this place in my database.
推荐答案
如果从某处获得指向数据库中某个位置的完整URL,则可以使用以下方法为该位置创建DatabaseReference
:
If you get a full URL to a location in the database from somewhere, you can create a DatabaseReference
to that location with:
String url = "https://<your-project>.firebaseio.com/path/to/data";
DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl(url);
因此,如果您的URL以https://<your-project>.firebaseio.com/
开头,则上述方法适用.
So the above works if your URL starts with https://<your-project>.firebaseio.com/
.
如果获得完整路径,则可以使用:
If you get a full path instead, you'd use:
String path = "/path/to/data";
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
这篇关于将firebase数据库url(String)分配给Database Reference变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!