I read the firebase documentation: https://firebase.google.com/docs/database/android/retrieve-data
And I am still confused, on how to properly query just 1 object from my Firebase database.
Here is the lines of code I have written.
private Firebase mRef;
And in on start:
#Override
protected void onStart()
{
super.onStart();
//Firebase
mRef = new Firebase("my database url");
}
Then in onCreate or in onStart() I use this line:
Query myQuery= mRef.child("key to my DataObject");
But I get an error in that line, and the solution it provides me is:
change or migrate myQuery to com.firebase.client.Firebase
I am wondering which Query am I suppose to import? I have currently imported:
import com.google.firebase.database.Query;
Also, when I query an object, does something in my database have to change in order for me to retrieve objects?
Hopefully someone can provide clear and explicit example code Or steps to show how you query an object (im assuming you can just write a for loop to get a list of objects back).
Thanks
Assume your Firebase database has below structure:
{
"users" : {...}
}
then, to get reference of users, you should code as:
DatabaseReference mDatabaseReferenceUsers; = FirebaseDatabase.getInstance().getReference("users");
Now, If your users object contains a list, then you need to use push() method to get unique key and then set value as below:
String key = mDatabaseReferenceUsers.push().getKey();
mDatabaseReferenceUsers.child(key).setValue(new User("Bob", 27));
Related
I dont know what is this thing is called but i want to get a refrence of it in android so i want to know what exactly is this thing is called , please check the image down
im taking about this line above "Quote" , whaat is this line is called and how do i get a databse refrence of it in android and really sorry for that bad handwriting though
right now i want to get refrence of 'Quote" im doing it like this
databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
but how can i get a refrence of that line above"Quote"
databaseReference = FirebaseDatabase.getInstance().getReference("What should i put here to get that refrence ?");
Seems you are using realtime database that is called firebase reference url
you can try as:
import { getDatabase, ref, child, get } from "firebase/database";
const dbRef = ref(getDatabase());
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
You can find your Realtime Database URL in the Realtime Database section of the Firebase console.
Depending on the location of the database, the database URL will be in one of the following forms: https:// DATABASE_NAME . firebaseio.com (for databases in us-central1 ) for more details on it you can access the documentation on
https://firebase.google.com/docs/database/web/read-and-write
If you want to get a reference to the root of the database, you can call getReference without any value:
rootReference = FirebaseDatabase.getInstance().getReference();
Also see the documentation for getReference() without parameters, which says:
public DatabaseReference getReference ()
Gets a DatabaseReference for the database root node.
Returns: A DatabaseReference pointing to the root node.
I'm new in development and I'm aware that stack isn't for 'full code requests'. But I'm stuck and can't find the solution.
I'm using Room database which has two columns - firstName and lastName. I'm loading the database with passed firstName parameter:
#Query("SELECT * FROM passenger WHERE firstName LIKE :firstName")
List<Passenger>getAllByName (String firstName);
It works as supposed.
But.. When I want to update Passenger, I need to populate data again, again, and again. There comes LiveData and observer.
But.. setValue in LiveData is private and I cannot send any parameters for Query line. There comes MutableLiveData, but how can I implement that?
#Eduardas Seems like you need to return LiveData instead:
LiveData<List<Passenger>> getAllByName (String name);
And you can write a transformation in the ViewModel or you can directly observe it from your Activity/Fragment.
In activity/fragment onCreate() or onResume():
YourDao.getAllByName(name).observe(this, new LiveData<List<Passenger>>(){
#Override
public void onChanged( #Nullable List<Passenger>) {
// update your adapter if the list isn't null
}
});
Something similar to above. You can add customization as per your use case.
I'm trying to learn how to connect my android application to the firebase database. I've tried the following code:
mDatabase = FirebaseDatabase.getInstance().getReference();
productCloudEndPoint = mDatabase.child("Sample");
productCloudEndPoint.push().setValue("Hello World");
However, nothing gets written when I look at the Firebase console. I have also set the rules.
{
"rules": {
".read": true,
".write": true
}
}
Am I missing something? Thanks
I had the same issue and the problem was that I was using the simulator. You have to use a physical device.
Do not use push method when you are using set() method, set() method would set the value to the reference.
ex:
<database-ref>/users/{UserId}/
username: {name}
Query would be:
mDatabase.child("users").child(userId).child("username").setValue(name);
In your case :
mDB.child("sample").setValue("Hello World");
While push method used to post new custom object, Firebase generates unique id and save into list of such objects.
NOTE: Push() method is used fro javaScript.
Example :
You have user object with properties like
Class User{
String name;
String email;
}
To save such object, you can simply use push() method.
Firebase will save it with a unique Id auto generated.
<db-ref>/users/{uniqueId}/
For JAVA: equivalent method is put() method.
I created my firebase console project and want to use real time database. I am able to add data using my model class (On Register Activity). But now i want to retrieve the same data in my HomeActivtiy.
Firebase is only providing valueChangeListener or addValueListener. But by the time i reach HomeActivity, and set the listener. Data has already changed and my Datasnapshot return null object on onDataChanged()
I am using Firebase version 3 - android
I want to do a get request to my "users" node, where I need data instead of just Database reference. is it possible in Firebase?
Are you sure your reference is correct? If you are using an addValueEventListener on the HomeActivity then your value should also change in the HomeActivity (I assume you are getting a list of registrations).
Make your reference point to parent node of where the registrations are happening.
You can also prove yourself correct or wrong by adding an addOnSuccessListener after saving the registration data.
db.updateChildren(childUpdates).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
finish();
}
});
If I have a DatabaseReference, get a Query from it, and then add a listener to that Query, do I have to call removeEventListener on the Query reference, or can I remove it from the original DatabaseReference?
It's best to use the exact same instance of DatabaseReference or Query that you used to add the listener. A Query derived from a DatabaseReference is not at all the same thing as the DatabaseReference itself - they represent different sets of data.