{
"key1" : {
"region": 2,
"text": "This is text"
},
"key2" : {
"region": 8,
"text": "This is text"
}.
"key3" : {
"region": 6,
"text": "This is text"
}
}
I want to get the JSON objects with a region of 2 and 8.
Try this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query q=ref.orderByChild("region").startAt(2).endAt(8);
q.addValueEventListener(..){//..}
Assuming the the random keys are direct children to the root node.
If you want to use a range of regions, meaning all region from 2 to 8, in which will be also included all regions like (3, 4, 5, 6 and 7), Peter's answer will work perfectly fine.
But if you want to use only region 2 and 8, I recommend you duplicate data. This is a quite common practice when it comes to Firebase and is called denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
In your case, you should consider augmenting your data structure to allow a reverse lookup like this:
regionTwoAndEight
|
--- "key1": true
|
--- "key2": true
I'm new to Android development but usually find the answer to my issues online. I've spent the whole weekend without success.
What I'm trying to do is read data from the JSON file on the internet.
It looks like this
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "9246.27",
"price_btc": "1.0",
"24h_volume_usd": "5659320000.0",
"market_cap_usd": "156378808114",
"available_supply": "16912637.0",
"total_supply": "16912637.0",
"max_supply": "21000000.0",
"percent_change_1h": "1.77",
"percent_change_24h": "-1.51",
"percent_change_7d": "-17.57",
"last_updated": "1520773466"
}]
It's just a single object in an array. Unfortunately, it's not up to me how it looks like.
What I am successful at doing is reading HTML and then finding the fields I'm looking for but I want to do it in a better way. I've watched tens of videos and read some tutorials online but I just can't make it work. I get exceptions, crashes and what not and out of the rage deleted it all and I am starting from scratch again. I know beginner questions are not usually welcomed so I don't ever ask anything myself. I'm just doing this little project for fun to learn some programming.
It would be cool if somebody could show or point me in the right direction. I literally can't even read the data if I'm approaching it as JSON and not HTML.
I'm hoping someone in the wide world of Android development has come across this problem/use case before, and has a relatively straight-forward approach for implementation.
I have a client Android application, that needs to render a list of fields that the end-user then fills out. Each field is a certain known-type, such as "Text" or "Radio", "MultiSelect", etc. When the user taps on a form, an API call is made to a backend which returns the schema for that form (ie: each field's UUID, title, description, hint text, etc), and the data for that form where some fields are likely already filled out from a prior time. Example of what data I would get over an API call:
{
"submittedBy": 8,
"updatedBy": 8,
"createdBy": 8,
"submittedDateMillis": 1489680600000,
"updatedDateMillis": 1489680600000,
"createdDateMillis": 1489680600000,
"name": "My Form",
"formTemplateId": 3,
"id": 0,
"schema": {
"6051c1e3-b4bf-4e6a-afe3-de2497dbff11": {
"units": "ft.",
"hintText": "Length of measurement",
"required": false,
"description": "Take the length of the measured item to 4 decimal places.",
"title": "Measurement",
"type": "number"
},
"fdf6ff0b-e60d-4591-a3e7-5467cd7bc67e": {
"enum": [
"Foo",
"Bar",
"Baz",
"Bat"
],
"required": true,
"hintText": "",
"description": "This is a description for a multiple choice question",
"title": "Multiple Choice (radio) title",
"type": "radio"
},
"203ef6d8-03fe-48e8-9a45-b18d12721d44": {
"enum": [
"Option 1",
"Option 2",
"Option 3",
"Option 4"
],
"required": true,
"hintText": "",
"description": "This is the description for a multiselect question",
"title": "This is the title for a multiselect question",
"type": "multiselect"
},
"751e9b8f-a59d-4e81-b3da-17ae44daa44e": {
"enum": [
"A dropdown answer",
"This is another option for a dropdown question it's limit is 130 characters"
],
"required": true,
"hintText": "",
"description": "This is the description for a dropdown question",
"title": "This is the title for a dropdown question",
"type": "select"
},
"33e13828-9171-4680-b68b-9838d4d42af8": {
"required": true,
"hintText": "This is the hint text for a text question limit 130 characters",
"description": "This is the description for a text question limit 5000 characters",
"title": "This is the title for a text question limit 130 characters",
"type": "text"
}
},
"fields": {
"6051c1e3-b4bf-4e6a-afe3-de2497dbff11": "5555.5555",
"fdf6ff0b-e60d-4591-a3e7-5467cd7bc67e": "Bar",
"751e9b8f-a59d-4e81-b3da-17ae44daa44e": "A dropdown answer",
"203ef6d8-03fe-48e8-9a45-b18d12721d44": [
"Option 1",
"Option 2",
"Option 4"
],
"33e13828-9171-4680-b68b-9838d4d42af8": "My answer for your text question."
}
}
The API call, say /api/v1/forms/0, returns the above data. In that I have schema which describes the field types, and fields which give me the answers to populate (some of which could be missing). They both have UUIDs which "match up", so I know what field data to put into what form field.
Now I have to render that Form, and allow the user to tap "Submit" and POST/PUT the new data back to the API.
What is an approach for dealing with this? I consider myself a beginner in Android, and from what I've come up with so far, is probably not the best solution (and probably doesn't scale beyond say, 50 questions, as the "render" and "submit" portions of this activity will become slow):
Make the API call, get the data (above example) back.
For every schema type, .inflate() an XML layout that is whatever that .type is (number, text, radio, etc), and construct a Java type (FormElement is what I'm calling it) that represents that schema JSON type. After .inflate(), .setTag(formElement) and "attach" that Java FormElement to it.
Get the widget inside that layout we just inflated, and if we have corresponding data from the fields mapping in the JSON, set the data to whatever that is. If not, leave it blank.
When the user taps "Submit", grab the Parent Form View, and get it's children. Loop through every child and pull out its FormElement via .getTag(). Get the FormElement#getType to find the type of the View, then work backwards and knowing the View we are iterating on, cast it, get it's inner data value, build the resulting JSON data back up, and PUT that to the API.
I might assign every Widget that represents a data entry point (Text, Radio, etc) a unique ID, based on the UUID from the schema (UUID is v1, so one way is to get the timestamp, and hope for the best, since we would be going from 128 bits to 32 bits). Then use this ID later, when I need to pull data out after the user taps Submit.
There looks to be some promising capability in Android's Data Binding Library, but I don't think Android's data binding can handle the "dynamic" nature of laying out this UI, with different Widgets that have different data types (some of these are Pull Down menus).
Is data binding a better approach here?
Can data binding handle both concerns of rendering the UI here, and helping fetch the data from that UI to ultimately compose my API PUT request back to the server?
Resources I've looked at so far, which shine some light on this overall problem:
http://www.mysamplecode.com/2011/10/android-dynamic-layout-using-xml-add.html for adding/inflating UI's based on pre-defined XML.
https://realm.io/news/data-binding-android-boyar-mount/ a really good tech talk by #george-mount that covers some basics of data binding.
.setTag() & .getTag() - What is the main purpose of setTag() getTag() methods of View? which seems to fit my use case, where I need to have the views "know" things about themselves (like where they came from in the JSON response).
Thank you all ahead of time!
You should create a representation of those fields in memory (like a Collection of Field's).
Then you can use RecyclerView to lay them out efficiently.
In RecyclerView, you can have view types (one for each Field type) that knows how to handle a particular field.
Inside the RecyclerView, to bind the views, you can use data binding. There is a demo on github that shows how to effectively use data binding with RecyclerView.
Last but not least, make sure your network operations are completely de-coupled from the UI. UI just reads the collection so each time you do a network request, it updates the collection, that notifies a change and RecyclerView will update itself. (you probably want to make optimistic updates on the collection since network requests may take time but that is a large topic to cover here).
I've been searching for different examples but unable to find proper solution for my firebase nodes. I have 3 nodes 1 for Question ,Second node contains Answers of questions and 3rd node contains Comments on that Answer.!
How I will be Able to perform query using firebase
I've been searching for different examples but unable to find proper solution for my fire base tables. I have 3 nodes
Questions
Answers
Comments on Answers
How I will be Able to perform query using firebase (Join based Concept Implementation)
mdatabaseReference.child("Answer").equalTo(QID);
how I will be able to get Answers of specific Question and Comments on that Answers.!
here is My JSON
{
"Answer" : {
"f40357b1-d1f5-4b7a-98ec-54d9e7b2e518" : {
"dateTime" : "16 Mar 2017 15:30:29",
"professorAnswer" : "Hezbollah is an Islamist religious organization founded in 1985 and based in Lebanon. It follows Shi'Islam (also called Shi'ite Islam), the second largest denomination of Islam. Shi'a Muslims follow the teachings of the prophet Muhammad, a direct descendant of Isma'il (the first son of Ibrahim/Abraham).Contd.!",
"professorId" : "7ceef713-eb59-4db4-a8d2-21d5a01eedfc",
"questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2"
}
},
"comment" : {
"29192e3a-a013-4fcc-9859-1f5cc62464cb" : {
"commentText" : "ORGANIZATION hezbollah bases on the bible but their goals is to save people in pagans work!",
"dateTime" : "16 Mar 2017 15:30:52",
"AnswerId" : "f40357b1-d1f5-4b7a-98ec-54d9e7b2e518"
"questionId" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
"userId" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
}
},
"questions" : {
"41c454a8-fab6-4e41-9093-b1120ffd1be0" : {
"description" : "I know they're a Islamic organization but where are they based? What are their goals?",
"idQuestion" : "fd2a34a0-e7d9-4b2c-8192-59705df827c2",
"time" : "16 Mar 2017 15:30:12",
"title" : "What are the aims of the religious organization Hezbollah?",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2",
}
},
"user" : {
"13bd37e5-fc87-4468-a89e-7cb4ecaab05f" : {
"email" : "email#gmail.com ",
"user_id" : "bXCeW6jfidbHuMCCCMkDGWcGZRS2"
}
}
The problem is that I want to filter those events... like for example, using .orderByChild("Answer").equalTo(QID) maybe it's wrong query but it is just for concept to get just the Answers for a given Question ID and then populate my List with it.
The right way to do this is to get every data separately, I don't know perfectly how your data are organized but this may be an appropriate solution:
// Gets the Question with id = QID
mdatabaseReference.child("Questions").child(QID);
// Gets the Answers for that question
mdatabaseReference.child("Answers").child(QID).once("value", function(answers) {
// For every Answer gets the comments
for(var answerID in answers) mdatabaseReference.child("Comments").child(QID).child(answerID);
});
EDIT: To use Firebase efficently you should structure your data depending on how you want to retrieve them. If you want to get all the answers for a given question I suggest you to use this data structure:
{
"Answers": {
"questionID": {
"answerID": {
"dateTime" : "16 Mar 2017 15:30:29",
"professorAnswer" : "Hezbollah is an Islamist religious...,
"professorID" : "...",
"questionID" : "..."
}
}
}
}
so that you can get your data for a given questionID this way:
mdatabaseReference.child("Answer").child(questionID).once('value', function(answers) {
// answers contains all the answer for the question with ID == questionID
});
NOTE: You don't have tables in Firebase, everything is JSON Object
There In No Concept of Joins In fireBase.Its noSql ..All you need to do is Query On According to Question Id and then in answer id check for question id..and same thing goes for comments it will be a nested EventBased search.!
I'd like to identify users beyond their username. Is there any way to identify users with a token/code/ID# that is static and will never change?
Simply because usernames are flexible and can be changed at any time meaning I cannot keep track users.
Dani you can track users by their user id, which stays static even if they change their username.
In Instagram's API example they use this endpoint: https://api.instagram.com/v1/users/{user-id}/?access_token=ACCESS-TOKEN
which gives the following response:
{
"data": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
"bio": "This is my bio",
"website": "http://snoopdogg.com",
"counts": {
"media": 1320,
"follows": 420,
"followed_by": 3410
}
}
Depending on what data or which endpoints you are hitting beforehand, you should be able to get to the user ID and use that accordingly throughout your application.