3

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.

1 Answer 1

17

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.