It is possible to retrieve in such a way data from Firebase? - android

I would like to know how can I retrieve the key of this object based on the object content. I'm using a realtime firebase database.
JSON code :
"Places" : {
"-M11-OIIHbKTVqCJ-Swv" : {
"name" : "Farmacia Pagani Dr. Roberto",
"others" : {
"Abilify" : 100,
"Acetaminophen" : 120,
"Acyclovir" : 140,
"Adderall" : 160
}
},
"-M11-OJJCGAPdJUknLXX" : {
"name" : "FARMACIA DR. TUMIATTI MARIANO",
"others" : {
"Abilify" : 100,
"Acetaminophen" : 120,
"Acyclovir" : 140,
"Adderall" : 160
}
}
I want to retrieve M11-OIIHbKTVqCJ-Swv if I have the name : Farmacia Pagani Dr. Roberto"
The code I wrote :
mUserDatabase = FirebaseDatabase.getInstance().getReference("Places");
Query query = mUserDatabase.orderByChild("name").equalTo(nameOfThePharmacy);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("The key is : "+dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
This code doesn't work, it returns : "Places" but I want the key of the object that contains the field name that equals with = ad example Farmacia Pagani Dr. Roberto , the output should be : -M11-OIIHbKTVqCJ-Swv

Related

Android Firebase Retrieving data as a list from multiple nods

I am using firebase realtime database in my first android app. I'm trying to retrieve data from firebase as an arraylist of "username" but i'm constantly having the Query returning a null result. i'm very new to firebase so i'm really struggling with data retrieval errors.
"Marchand" : {
"Taha" : {
"adress" : "58 avenu St Eugène ",
"blockage" : false,
"latitude" : 35.7498635,
"longitude" : -0.5566705,
"mail" : "mailmail#gmail.com ",
"password" : "123456",
"signalement" : 0,
"telephone" : "0666666666",
"username" : "Taha"
},
"Yasmine" : {
"adress" : "Address kkdndbdk ",
"blockage" : false,
"latitude" : 35.7498636,
"longitude" : -0.5566704,
"mail" : "randommail#gmail.com",
"password" : "bobo",
"signalement" : 0,
"telephone" : "06999999",
"username" : "Yasmine"
}
.
.
.
This is one of my attempts, i tried to use the order-by-child method to iterate through the multiple nods that i have but i'm sure that it's not the right way to do this, this gives me null poiter exceptions because the datasnapshot is returning a NULL value :
referenceMarchand = rootNode.getReference("Marchand");
final ArrayList<String> types = new ArrayList<>();
Query users = referenceMarchand.orderByChild("username");
users.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
types.add(dataSnapshot.child("username").toString());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
lets make use of some functions of firebase, Lets see it works for you.
part 1)
we will save it another way around
"Merchant" : {"Name" : "Taha"
"ID" :"-KSMCMCMnnkd"},//Firebase generated id
{"Name" : "Yasmine"
"ID" :"-KSMCMccccnkd"},//Firebase generated Id
...........
Part 2)
And Save the Details
"Details" : {
"ID" : "-KSMCMCMnnkd" //Firebase generated id
"adress" : "58 avenu St Eugène ",
"blockage" : false,
"latitude" : 35.7498635,
"longitude" : -0.5566705,
"mail" : "mailmail#gmail.com ",
"password" : "123456",
"signalement" : 0,
"telephone" : "0666666666",
"username" : "Taha"},
{
"ID":"-KSMCMccccnkd" //Firebase generated id
"adress" : "Address kkdndbdk ",
"blockage" : false,
"latitude" : 35.7498636,
"longitude" : -0.5566704,
"mail" : "randommail#gmail.com",
"password" : "bobo",
"signalement" : 0,
"telephone" : "06999999",
"username" : "Yasmine"}
In Part 1)
DatabaseRefrence ref = FirebaseDatabase.getInstance("/Path");
String id = ref.child("Merchant").push().getKey(); //Firebase generated id
Merchant merchant = new Merchant();
merchant.setId(id);
merchant.setName("SomeName");
ref.child("Merchant").child(id).setValue(merchant);
Part 2)
Detail detail = new Detail();
detail.setAddress("Some Address");
detail.setBlockage("false");
detail.setLatitude("SomeValue");
detail.setEmail("someText#gmail.com");
......
ref.child("Details").child(id).setValue(detail);
Now
List<Merchant> merchants = new ArrayList<>();
List<Detail> details = new ArrayList<>();
ref.child("Merchant").addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(Datasnapshot snapshot:dataSnapshot){
Merchant merchant = snapshot.getValue(Merchant.class);
merchants.add(merchant);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
ref.child("Details").addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(Datasnapshot snapshot:dataSnapshot){
Detail detail = snapshot.getValue(Detail.class);
details.add(detail);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Now you can get the details for some merchant or save it in local database for later querying.

Is there any query to retrieve the data present in list firebase android

I want to retrieve only that post where postCategory matches the user interest.
How can I achieve this type of query. Any Help??
Below is my json file of database..
"Posts" : {
"-MAKobGjdhMfJFwZ1kol" : {
"postCategory" : "Animals",
"postDesc" : "still",
"postImage" : "Image Link",
"postPersonName" : "Name",
"postPersonUid" : "CLQA22uZpLS1ErIrnoBTrC3xqBw2",
"postTitle" : "ek"
},
"Users" : {
"CLQA22uZpLS1ErIrnoBTrC3xqBw2" : {
"email" : "email",
"interest" : {
"-MA14zFcNfs1JAjux129" : {
"name" : "Developer"
},
"-MA14zFgzhL-MOBB7D6h" : {
"name" : "Shopping"
}
},
"name" : "Name",
"uid" : "CLQA22uZpLS1ErIrnoBTrC3xqBw2"
}
In firebase database you cannot have such queries.
You should use Firestore (https://firebase.google.com/docs/firestore) for such advanced queries.
firestore/query-data
firestore
To get all posts with postCategory equal to Animals, you'd do:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
Query query = ref.orderBy("postCategory").equalTo("Animals");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Log.i(TAG, postSnapshot.getKey()+": "+postSnapshot.child("postTitle").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
If you want to retrieve posts for multiple categories, you'll have to perform a separate query for each of those and merge the results on your client.
I'd also recommend changing how you store the categories for a user. The idiomatic way is:
"Users" : {
"CLQA22uZpLS1ErIrnoBTrC3xqBw2" : {
"email" : "email",
"interest" : {
"Developer": true,
"Shopping": true
}
},
The reasons this is more common is that it automatically ensures that each value can occur only once in the interest object, since property names are by definition unique.

Firebase equalTo() couldn't work correctly

I'm developing an Android app with the following Firebase database table:
"fav" : {
"-Ktf0tmShJ48-oH76Or3" : {
"article" : {},
"articleId" : "1005",
"id" : 1505034214723,
"userId" : "xUdklm4VMFa9FJvA15XvlIkYKFA3",
"userId_articleId" : "xUdklm4VMFa9FJvA15XvlIkYKFA31005"
},
"-Ktf13BfglsB4sFhHv-P" : {
"article" : {},
"articleId" : "1001",
"id" : 1505034257363,
"userId" : "xUdklm4VMFa9FJvA15XvlIkYKFA3",
"userId_articleId" : "xUdklm4VMFa9FJvA15XvlIkYKFA31001"
}
}
I want to query by the string "userId_articleId" like this:
FirebaseDatabase.getInstance().getReference().child("fav").orderByChild("userId_articleId").equalTo(firebaseUser.getUid() + article.id)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot data : dataSnapshot.getChildren()){
fav = data.getValue(Fav.class);
favKey = data.getKey();
starItem.setIcon(R.drawable.star);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i("fyales","The dataChanged");
}
});
}
But I can't get the Object I wanted, The weirdest thing is that I can get the Object I want correctly by the key "articleId" or "id". It can also work if if I give the definite value "xUdkl" to "userId_articleId" if the "userId_articleId" in the database is the value "xUdkl" like this:
.orderByChild("xUdkl").add....
But it can't work if I give the value "xUdklm4VMFa9F" to "userId_articleId" if the "userId_articleId" in the database is "xUdklm4VMFa9F" like this.
orderByChild("xUdklm4VMFa9F").add...
I don't know why.It is related the length of String or it's just a bug in firebase ? Any help will be appreciated.
I solved the problem...The reason is that I didn't keep the data fresh . So I synced the data before I used them by ref.keepSynced(true).You can get the answer in
Enabling Offline Capabilities on Android.

What is construction of Json DB for Android Firebase Realtime Database?

I uploaded such json to firebase console of realtime database. And to get some "words" im trying get child "words" with orderByChild ... startAt and so on...
Someone on this forum told me there is no any child in "words". Whats wrong?
{
"words" : {
"1" : {
"id" : 1,
"lang1" : "s1",
"lang2" : "d1",
"type" : "a"
} ,
"2" : {
"id" : 2,
"lang1" : "s2",
"lang2" : "d2",
"type" : "a"
} // ...
}
}
And my Java code:
Query fb = FirebaseDatabase
.getInstance()
.getReference()
.child("words")
.orderByChild("lang1")
.startAt("s2")
.limitToFirst(20);
BTW. I can not get any responce. This listener does not return anything.
fb.add... ValueEventListener() {
... void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Word w = singleSnapshot.getValue(Word.class);
Log.i(TAG, new Gson().toJson(w));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});

Android - Save list of object property with specific key on Firebase

Let's consider a "UserBean" with a property "emails" that is an ArrayList<EmailBean>.
When saved in Firebase it's like :
"users" : {
"$userID" : {
"emails" : [
"0" : {
"type" : "first",
"address" : "google#google.com",
"private" : true
},
"1" : {
"type" : "second",
"address" : "firebase#firebase.com",
"private" : false"
}
],
"name" : "Mitch"
}
}
So is it possible to achieve something like this :
"users" : {
"$userID" : {
"emails" : [
"first" : {
"address" : "google#google.com",
"private" : true
},
"second" : {
"address" : "firebase#firebase.com",
"private" : false
}
],
"name" : "Mitch"
}
}
I would prefer to not having to save each address one at a time.
Maybe there's an annotation to tells Firebase to use this property as a key ?
Regards.
I recomend you using the code below:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference emailsRef = rootRef.child("users").child(userId).child("emails");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String type = ds.child("type").getValue(String.class);
ds.child("type").getRef().getParent().setValue(type);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
emailsRef.addListenerForSingleValueEvent(eventListener);
What have i tried with this code? So in order to achieve what you want, first of all we got the value of your type key. Then we get a step up in the tree and set the partent with the value of your type key for each particular node.
Having the value of type in stead of 0, 1 and so on, to get the work done, just remove the type key, from each node using removeValue() method directly on the reference.

Categories

Resources