Shachaf Zohar
Shachaf Zohar

Reputation: 55

Assign a firebase database url(String) to a Database Reference variable?

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.

Update (from comment to Alex's answer):

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.

Upvotes: 3

Views: 7547

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598775

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);

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);

Upvotes: 17

Related Questions