I have this JSON structure on my FireBase Database:
{
"companies": [{
"name": "Soft",
"position": "Developer",
"location": "NY",
"start": 1462060800,
"end": 1470009600,
"description" : ""},
{
"name": "Xpert",
"position": "Developer",
"location": "London",
"start": 1456790400,
"end": 1462060800,
"description" : ""},
{
"name": "AXinformation",
"position": "Developer",
"location": "Paris",
"start": 1388534400,
"end": 1456790400,
"description" : " "},
.....
.....
.....
I am trying to get that data doing something like on my code
public void onCallingFireBaseExperience() {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("companies");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
Log.d(Constans.LOG, dataSnapshot1.child("name").getValue().toString())
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.d(Constans.LOG, "Failed to read value.", error.toException());
}
});
}
But I am always facing a memory location on my LogCat
.google.firebase.database.DataSnapshot$1#f264efd
What I am doing wrong? How I can iterate the data?
It is because Firebase doesn't support storing arrays natively,
Firebase has no native support for arrays. If you store an array, it
really gets stored as an "object" with integers as the key names.
Please check this link from the official Firebase blog to get more information on why arrays are not supported and how you should properly structure your data to make up for it.
So, basically the way you have structured your data is not correct according to Firebase, because of which dataSnapshot.getChildren() doesn't give you any children at all.
Related
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
usersList.clear();
for (DataSnapshot ds: snapshot.getChildren()){
ModelUsers modelUsers = ds.getValue(ModelUsers.class);
if (!modelUsers.getyearSection().equals(fUser.getUid())){
usersList.add(modelUsers);
}
adapterUsers = new AdapterUsers(getActivity(), usersList);
recyclerView.setAdapter(adapterUsers);
}
}
This is my Firebase Database Users
{
"Users": {
"7xWet0t6jZYcajSpt70nqbQy29q1": {
"Password": "randomber16",
"email": "klmartisano#gmail.com",
"fullName": "Kenneth L. Martisano",
"uid": "7xWet0t6jZYcajSpt70nqbQy29q1",
"usertype": "Student",
"week5score": "4",
"yearSection": "4-2"
},
"aMx87W9b0vaQTwocAqKzuEvGj0G3": {
"Password": "randomber16",
"email": "srosarda#gmail.com",
"fullName": "Sean Harvey C. Rosarda",
"uid": "aMx87W9b0vaQTwocAqKzuEvGj0G3",
"usertype": "Instructor",
"yearSection": "4-2"
},
"q78J0so5mgdIBlTfMRSxWqXuxPj1": {
"Password": "randomber16",
"email": "cambroneroanne09#gmail.com",
"fullName": "Anne Cambronero",
"uid": "q78J0so5mgdIBlTfMRSxWqXuxPj1",
"usertype": "Student",
"yearSection": "4-2"
}
},
im new to android studio and im asking if there's a way to this to diplay users info using child value "yearSection" and current user "yearSection"
Im trying to call users in firebase database using child value of "yearSection" and display users with specific yearSection that equals to the current users "yearSection"
It sounds like you're trying to order and filter data, which would look something like this for your data structure:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
Query query = ref.orderByChild("yearSection").equalTo("4-2");
query.addValueEventListener(new ValueEventListener() {
...
Of course you'll have to make sure you pass the correct value to equalTo for your use-case.
{
"City" : {
"New York" : {
"City Name" : "New York",
"Place to visit" : {
"Times Square" : {
"Address" : "Somewhere",
"Name" : "Times Square"
},
"Central Park" : {
"Address" : "On America",
"Name" : "Central Park"
}
}
},
"Los Angeles" : {
"City Name" : "Los Angeles",
"Place to visit" : {
"Hollywood" : {
"Address" : "Up There",
"Name" : "Hollywood"
},
"Beach" : {
"Address" : "By the sea",
"Name" : "Beach"
}
}
}
}
}
Hi, I'm a little bit new to NoSQL database especially Firebase. If I structured my data like this, how can I get the name of "place to visit" where "City name" is New York?
And I want the result (Times Square and Central Park) to be shown as ListView. How can I achieve that?
Or is there any better way to restructure my data so I can query my desired output easier?
First of all city should hold a list of items (in your case city name "New York or Los Angeles").
By this it will be easier for you to traverse through the list and fetch desired city as required in a fastest and efficient manner, this is because you will get inbuilt methods to traverse.
I have also restructured your json response in order to make it a list of objects and removed redundant variables
Below is the response
{
"City": [{
"New York": {
"Place to visit": {
"Times Square": {
"Address": "Somewhere",
"Name": "Times Square"
},
"Central Park": {
"Address": "On America",
"Name": "Central Park"
}
}
}
},
{
"Los Angeles": {
"Place to visit": {
"Hollywood": {
"Address": "Up There",
"Name": "Hollywood"
},
"Beach": {
"Address": "By the sea",
"Name": "Beach"
}
}
}
}
]
}
Because the name of the city which you want to use in your query is already a node in your Firebase database, to get those names, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference placeToVisitRef = rootRef.child("City").child("New York").child("Place to visit");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String placeToVisit = ds.getKey();
Log.d("TAG", placeToVisit);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
placeToVisitRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
Times Square
Central Park
Try this one,
DatabaseReference ref= FirebaseDatabase.getInstance().getReference();
DatabaseReference refPlace= rootRef.child("City").child("New York").child("Place to visit");
refPlace.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> dataSnapshotsChat = dataSnapshot.child("articleTags").getChildren().iterator();
while (dataSnapshotsChat.hasNext()) {
DataSnapshot dataSnapshotChild = dataSnapshotsChat.next();
// check dataSnapshotChild, here you will get the details under Place to visit
Object name = ds.child("Times Square").getValue(Object.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I made a Firebase Realtime Database and integrated it with my Android app. However, I am not able to filter results based on a field.
This is my database:
{
"news": {
"15211": {
"cat_id": 3,
"cat_name": "ലോകം",
"description": "ഒരുപക്ഷെ ജിഹാദികൾ പാകിസ്താന്റെ ഭരണം അട്ടിമറിച്ചു ...",
"id": 15211,
"image": "http://d3il9y9grvji6c.cloudfront.net/57ee50db0321d.jpg",
"item_type": 1,
"time": "1475236130",
"title": " പാക്കിസ്ഥാന്റെ ന്യൂക്ലിയർ ചാവേറുകൾ ഇന്ത്യയെ ആക്രമിക്കുമോയെന്ന് ഹിലറിക്ക് പേടി",
"url": "http://www.manoramaonline.com/news/just-in/hillary-clinton-fears-nuclear-suicide-bombers-from-pakistan.html",
"url_text": "മനോരമ"
},
"15382": {
"cat_id": 4,
"cat_name": "കായികം",
"description": "ന്യൂസിലന്ഡിനെതിരായ രണ്ടാം ക്രിക്കറ്റ് ടെസ്റ്റില് ...",
"id": 15382,
"image": "http://d3il9y9grvji6c.cloudfront.net/57f0fda3abe93.jpg",
"item_type": 1,
"time": "1475411410",
"title": "ഈഡനില് ഇന്ത്യ വിജയം മണക്കുന്നു; ലീഡ് 339 ",
"url": "http://www.reporterlive.com/2016/10/02/302271.html",
"url_text": "റിപ്പോർട്ടർ"
}
}
}
The code I am using to get all items with cat_id = 3 is
DatabaseReference ref = database.getReference("news");
final Query queryRef = ref.orderByChild("cat_id").equalTo(3).limitToLast(10);
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
...
}
...
}
However, this always returns no result. What could be the issue?
I'm able to get the data for a particular value in the child using orderByChild and equalTo (cool that it works for nested child as well)
private void getData() {
try {
final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child(Constants.TABLE_TASKS).orderByChild("user/id")
.equalTo("somevalue")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Timber.d(dataSnapshot.toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
Is there an easy way to get the data where a particular value is not found, basically something like a notEqualTo("somevalue") ?
In the Firebase Query model you can not filter for inequality to a value.
But you can test for the absence of any value (essentially: the absence of a property). For example with this data model:
{
child1: {
"user": {
"id": 1,
"name": "puf"
}
},
child2: {
"user": {
"id": 2,
"name": "abe"
}
},
child3: {
"user": {
"id": 3
}
}
}
I can query for children without a user/name property with:
ref.orderByChild('user/name').equalTo(null)
Which leads to the only child that doesn't have a name:
child3
Feel free to play with my jsbin to see if you get further: http://jsbin.com/liyibo/edit?js,console
Update: I knew I'd answered this before, but couldn't find it earlier. Here's the dupe: is it possible query data that are not equal to the specified condition?. It looks like I have a mistake in there, since clearly I'm testing for the absence of a property in the above code.
I think I've found the solution and this is more of how the database should be designed and actually now I understood the intention behind Firebase guideline
https://firebase.google.com/docs/database/android/structure-data
Original Design:
{
child1: {
"user": {
"id": "id1",
"name": "puf"
}
},
child2: {
"user": {
"id": "id2",
"name": "abe"
}
},
child3: {
"user": {
"id": "id1"
"name": "puf"
}
}
}
Updated Design:
So apart from the storing the id and name of the user, we should also store a node with the id itself as the key and mark it to true
{
child1: {
"user": {
"id": "id1",
"name": "puf"
"id1": true
}
},
child2: {
"user": {
"id": "id2",
"name": "abe"
"id2": true
}
},
child3: {
"user": {
"id": "id1"
"name": "puf"
"id1": true
}
}
}
With the updated design, if i execute ref.orderByChild('user/id1').equalTo(true)
I would get output as Child1 and Child 3
and if i execute ref.orderByChild('user/id1').equalTo(null),
I would get Child2 as the output
I'm developing an Android app with the following Firebase database table:
"posts": {
"id_1": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 0,
"visible": true
},
"id_2": {
"author": "google:111527135678918251524",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 2,
"visible": true
},
"id_3": {
"author": "google:111527135678918251124",
"color": -2960686,
"creationTime": 1427104145195,
"text": "my text",
"title": "my Title",
"type": 1,
"visible": true
}
}
I'd like to be able to retrieve a the posts sorted by a child (sorted by type as an example) and be able to retrieve all the posts with a specific child value (type = 1).
Reading the Firebase docs it seems I have to write the following code, but I don't get the wanted result.
Order by type
rootRef.child("posts").orderByChild("type")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = (HashMap<String, Post>) dataSnapshot.getValue();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
I get: an unordered Map of Posts
Type = value
rootRef.child("posts").orderByChild("type").equalTo(1, "type").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
result = dataSnapshot.getValue();
result = result;
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
})
I get: null.
My Datasnapshot object has key = "posts" and value = null
About the ordering, I think the problem is that I'm retrieving data as a HashMap which is itself unordered.
As suggested in the comments the second case needs .equalTo(1) instead of .equalTo("type",1) because it's preceded by the .orderBy() method.