Trying to fetch related data from firebase
I am new in Android and I am stuck here, want to retrieve data from Firebase where "del_id = 1" and "date = 2019-3-18" but I don't know how to write as Firebase way to retrieve what I want, Image above will further demonstrate what I have tried and what I want.
Your answer will be highly valuable for me.
The following line of code:
invSnapsjot.getValue(Invoice.class)
Returns an object of type Invoice and not a DataSnapshot object so you can call .child() method on it.
If you want to get a specific element (del_id = 1), you should simply use a query that looks like this:
myDatabase.orderByChild("del_id").equalTo("1").addValueEventListener(/* ... */);
Unfortunately, Firebase realtime database does not support queries on multiple properties but there is still a workaround for that, so please see my answer from the following post:
How to sort Firebase records by two fields (Android)
If you want to write or update value at that time you need to take .addValueEventListener
If you want to only set your value in firebase you directly set value without using .addValueEventListener
More details refer this link
https://firebase.google.com/docs/database/android/read-and-write
Related
I want to save the data on Firebase, that a user describes on an Intent. The saved data should be used to find one or more matching users with similar informations.
Thanks in forward.
Looks like you are building an app where you can clusters users with similar interests about particular thing, i would suggest you to use firebase firestore for storing data, and you can retrieve data that are similar using the firebase simple and compound query depending on your requirement.
So I am wondering about how necessary it is to store variables in the realtime firebase database if I want all Users at access the same dynamic variable.
So for instance, I have a arraylist that stores the list of open games, and if I want this list to update in realtime for every user should this List in firebase realtime database?
Sorry for the simplicity of the question
Yes, it may be a simple question, but it surely pops in head of everyone, once.
I think for updating any list dynamically in real time, would require you to access any kind of database.
It is not necessary to have it on Firebase database, but any database online, that can tell every open instance of your app that the list has to be updated at a particular instant.
The main reason of why you need it to be on database is updating it in real time and that too dynamically.
If it's not dynamic, meaning the content that you need, can be hardcoded then one way would be placing everything you need in your code and using timer or something like that to fire at particular moments to update things in your app.
Also that aside, sorting, storing and changing data is much simpler using a database, which also becomes one more reason for you to use a database like Firebase to keep content of your app that has to be updated frequently in real time.
You can know more about database in this Google link, I found.
I have this structure of Firebase, and I am trying to update the Author in all post-comments when the user updates his nick_name.
I used this query in Android but it didn't fetch all the data that I need:
mDatabase = FirebaseDatabase.getInstance().getReference("DataBase").child("post-comments");
Query query = mDatabase.orderByChild("uid");
and down here you will find a picture of the Firebase structure:
As I understand from your comments, you trying to change the named of this node -KykDRTQ5GToIe6oh_kA to commentID. The short answer is no, there is no way in which you can change the names of the nodes from your Firebase database. There is no API for doing that. What can you do instead is to attach a listener on that particular node and get the DataSnapshot object. Having that data, you can write it in another place using another name. In the end just remove the old node. So you cannot simply rename -KykDRTQ5GToIe6oh_kA to commentID.
I have a Firebase Realtime Database with a set of Items who related to locations on a Googlemap. So every child in the Firebase have a data structure like this.
-item
latitude:"5511231165654"
longitude:"516891812013216"
postalcode: "10013"
cityname: "New York"
In my app every item from the database, where added to the related location on the Googlemap in form of a marker.
Now i think it is not a good idea to send all Items to all users. It´s more efficiently to send the user´s only these items that are relevant for them. These items are that one who are in the same area with the user, in this case same area means same postal code.
To handle this i build this Query
Query sortedByLocation = myRef.orderByChild("postalcode").equalTo(currentpostalcode);
sortedByLocation.addListenerForSingleValueEvent(
new ValueEventListener() {
.
. do something
.
}
i got the currentpostalcode from a Geocoder also the cityname. Now the problem is sometimes the Geocoder can´t decode the postal code and return null for it, but it allways return a cityname. So i changed the query to
Query sortedByLocation = myRef.orderByChild("cityname").equalTo(currentcity);
So how can i combine these two querys ? I want to do something like this:
is postalcode == null than check for cityname
The other Question will be, it is possible to retrieve data manually at need ?
Imagine the user start the app, the app now get all items who are in the same area with the user, but now the user left this area and go into a other area.
How can i trigger to retrieve the new data for this new area without to restart the app ?
Firebase Queries can only contain a single sort/filter condition.
Sometimes you can combine multiple values into a single property to get the behavior you want. For an example of that, see my answer here: Query based on multiple where clauses in firebase
But in general it sounds like you might be better off using GeoFire, which uses GeoHashes (a way of combining longitude and latitude into a single property) to allow filtering items based on their distance to a point. Note that at this moment, the updated version of GeoFire for Java is still in the works.
Does anyone know how I can do a common "OR" like in a where clause, in firebase?
I need to do that in the query, because I am sending the query to an adapter. So, i mean, I cannot add a listener and check one value and then another. I need to have the complete query pointing to that result in my query.
What I have is something like:
chat1:
user1Id: "1"
user2Id: "2"
bothUsers: "1_2"
chat2:
user1Id: "2"
user2Id: "4"
bothUsers: "2_4"
I need to get all the chats of the user whose id is "2". I am trying to do a query like:
userLogged = 2;
Query queryRef = firebase.orderByChild("user2Id").equalTo(userLogged);
However it will only get the chats when the user 2 is in the user2Id position. But in the example above, when the user 2 is in the user1Id (chat2) it won't get. And I need to get it. How can I do this?
In a NoSQL database you will often have to model your data for the way your app needs it (see this article for a good explanation on NoSQL Data Modeling). So if you need a list of all chats for a specific user, store that list:
/userChats
user1Id
chat1: true
user2Id
chat1: true
chat2: true
user4Id
chat2: true
This process is called denormalizing and it is covered in the article I linked above, in the Firebase documentation on structuring data and in this blog post.