Firebase android query with more than one "EqualTo" - android

This is my Firebase structure:
"Braccianti" : {
"-KehahFy7z2IrUdchwxk" : {
"Associazione" : "-KehahFy7z4ngUdchwxk",
"Cellulare" : "3402659753",
"Cognome" : "Prova",
"DataDiNascita" : "12/04/1985",
"Nome" : "Bracciante"
},
"ashaosdfao" : {
"Associazione" : "gsdfskjdfsdf",
"Cellulare" : 234235246,
"Cognome" : "Example",
"DataDiNascita" : "10/12/2000",
"Nome" : "exam"
}
"dfsdfsdfsdf" : {
"Associazione" : "ewsvbdtgsdgf",
"Cellulare" : 23523975553,
"Cognome" : "Ex",
"DataDiNascita" : "17/01/1994",
"Nome" : "ghjghj"
}
}
I'd like to get "dfsdfsdfsdf" and "-KehahFy7z2IrUdchwxk" with a single query, so i thought about doing like this:
Query QueryAssociazioni = FirebaseDatabase.getInstance().getReference("Aziende/"+main.getNomeAzienda()+"/Associazioni");
QueryAssociazioni = QueryAssociazioni.equalTo("dfsdfsdfsdf").equalTo("-KehahFy7z2IrUdchwxk");
But it doesn't work.
How can i solve my problem?

Firebase doesn't allow multiple queries, but you can always add a ChildEventListener and check if the child's value :
QueryAssociazioni.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String child = dataSnapshot.getValue().toString();
if(child.equals("dfsdfsdfsdf") || child.equals(("-KehahFy7z2IrUdchwxk"))){
//Do something with the value
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

QueryAssociazioni.child("Braccianti")
.orderByChild("Associazione") //key name
.equalTo("Associazione") //value
Try this way...
Thanks.

Related

Android - Getting an Object from DataSnapShot - firebase database

I created a helper class including the functions and processes I would like to perform over my data in FirebaseDatabase.
The following function was meant to get all Posts in posts Node in fireabseDataBase:
public static Jobs getAllJobs() {
Posts posts= new Posts();
postsDBRef= FirebaseDatabase.getInstance().getReference("posts").getRef();
postsDBRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot jobSnapshot: dataSnapshot.getChildren()) {
// TODO: handle the post
String key = jobSnapshot.getKey();
//here
Post post= jobSnapshot.child(key).getValue(Post.class);
// and here
posts.add(post);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i(getClass().getName().toString(), ": " + databaseError.getMessage());
}
});
return posts;
}
in the onDataChange function I am trying to loop over the data and retrieve it, although while logging I do get data but the post object keeps giving Null
Here's How the data look like in firebase DataBase
{
"posts" : {
"-Kr9-ii-ArpC3fLuuGnb" : {
"address" : "sfhhfg",
"applied" : false,
"currency" : "le",
"description" : "ehdhyf",
"latitude" : 0,
"longitude" : 0,
"noOfWorkers" : 2,
"rating" : 0,
"reported" : false,
"salary" : "1250",
"title" : "rgchu",
"userId" : 0
},
"-Kr94BDkdEZrmxmjxv_Z" : {
/../
},
"-Kr9Dz3S0BQEYv4VO2l3" : {
/../
},
"-Kr9XqDPUvCRcv0lwFy_" : {
/../
}
}
}
and here’s what am getting from the Debugger in a single loop
DataSnapshot { key = -Kr9-ii-ArpC3fLuuGnb, value = {address=sfhhfg,
rating=0, title=rgchu, reported=false, description=ehdhyf, userId=0,
longitude=0, noOfWorkers=2, latitude=0, currency=le, applied=false,
salary=1250} }
Any idea what am I doing wrong?
After several trials
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Job job = snapshot.getValue(Job.class);
System.out.println(job.getTitle());
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i(getClass().getName().toString(), ": " + databaseError.getMessage());
}
});
Using .addChildEventListener was the only Solution for my issue, Still investigation the reason.
i have edited your code and it should work fine now, i have used the same code snippets in several projects before. make sure your pojo matches the json data, you can use this tool to make it easier for yourself.
and the most important thing , Don't use Firebase as functions that return values - it goes against it's asynchronous nature.make Firebase perform it's task and then within the closure (block) go to the next step.
P.S: before using ValueEventListener or ChildEventListener read the difference between them from here.
public void getAllJobs() {
Posts posts= new Posts();
postsDBRef= FirebaseDatabase.getInstance();
postsDBRef.child("posts").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot jobSnapshot: dataSnapshot.getChildren()) {
Post post= jobSnapshot.getValue(Post.class);
posts.add(post);
}
// go to next step from here e.g handlePosts(posts);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i(getClass().getName().toString(), ": " + databaseError.getMessage());
}
});
}

Firebase orderbychild().equalto() showing null

This is my JSON Structure:
{
"users" : {
"userKey1" : {
"deviceToken" : "deviceTokenID",
"displayName" : "Name here",
"dob" : "28/6/2017",
"email" : "efg#wxy.com",
"gender" : "M"
},
"userKey2" : {
"deviceToken" : "deviceTokenID",
"displayName" : "Name here",
"dob" : "28/1/2017",
"email" : "abc#xyz.com",
"gender" : "M",
}
}
}
In my android Firebase file, I type this:
Query findUser = mDatabase.child("users").orderByChild("email").equalTo("abc#xyz.com");
findUser.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i("findUser","Snapshot: " + dataSnapshot.toString());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I get the following log:
06-28 22:06:50.190 30962-30962/in.sample.project I/findUser: Snapshot: DataSnapshot { key = users, value = null }
I do not know why I am getting null when cleary I have a child who's email node matches "abc#xyz.com".
You need a foreach loop to get the user in your findUser query because your query contains a collection.
Query findUser = mDatabase.child("users").orderByChild("email").equalTo("abc#xyz.com");
put this part in your eventlistener
....
public Iterable<DataSnapshot> getChildren (){
for (DataSnapshot child : parent.getChildren()) {
....
}
}
Needed to add security rules.
rules: {
"users" : {
".indexOn": "email",
}
}
I guess, that did the trick. Hope it helps someone. :)

Firebase get all key

HI everyone I want to catch all the keys on my firebase database:
Users-
26dfg678-
Name: jack
Job: Farmer
43jkhjh4-
Name: bill
Job: ICT
I want to catch the id: 26dfg678 and 43jkhjh4 and put them in an array. This is my code:
final DatabaseReference database_nomi = firebaseDatabase.getReference().child("Users");
database_nomi.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//here
**name[0] = dataSnapshot.getKey();
Provee.setText(name[0]);**
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
if i do this it takes only the last but i want all..
To do this you need to read through your Firebase snapshot using a ValueEventListener (and addListenerForSingleValueEvent).
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
int i = 0;
for(DataSnapshot d : dataSnapshot.getChildren()) {
name[i] = d.getKey();
i++;
}
}
}//onDataChange
#Override
public void onCancelled(DatabaseError error) {
}//onCancelled
});
This code uses a foreach to read all dataSnapshot children, saving its key in an array at each iteration.

How can i get each child? Firebase getValue [android]

How could i get all the childs from my database?
This code get the first Element, how can i get Element_2,3 and successive? The best i accomplised is serialize the first country and print it.
My Database:
{
"Capitals" : {
"Country_1" : {
"Country" : "Macedonia",
"Capital" : "Skopje"
},
"Country_2" : {
"Country" : "Madagascar",
"Capital" : "Antananarivo"
},
"Country_3" : {
"Country" : "Malawi",
"Capital" : "Lilongwe"
}
}
My Code
#Override
public void onStart(){
super.onStart();
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference capitalsRef = database.getReference("Capitals");
compresoresRef.orderByChild("Ref").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
countryModel cm = dataSnapshot.getValue(countryModel.class);
String country = cm.getCountry();
String capital= cm.getCapital();
textData1.setText(country);
textData2.setText(capital);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The dataSnapshot while executing looks like:
DataSnapshot { key =Country_1, value = {Country=Macedonia, Capital=Skopje} }
You should use an ArrayList for store all childs that you get from Firebase using addChildEventListener
ArrayList<countryModel> countryList = new ArrayList<countryModel)();
compresoresRef.orderByChild("Ref").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
countryModel cm = dataSnapshot.getValue(countryModel.class);
countryList.add(cm);
...
}
}
Or if you use addValueEventListener for retrieve data, it will look like
compresoresRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
countryList.clear();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
countryModel cm = postSnapshot.getValue(countryModel.class);
countryList.add(cm);
}
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
Try this
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot post:dataSnapshot.getChildren())
{
countryModel countryModel cm = post.getValue(countryModel.class);
}
}

Querying Firebase on Android

This is my data:
{
"users" : {
"00c49c66-7ac8-9a48-7c4541d8bac6" : {
"email" : "d#gmail.com",
"gender" : "m",
"name" : "deepak",
"user_id" : "00c49c66-7ac8-9a48-7c4541d8bac6"
},
"1cb9c1de-1fcf-a2cc-bf78c84e03c8" : {
"email" : "e#gmail.com",
"gender" : "f",
"name" : "ekta",
"user_id" : "1cb9c1de-1fcf-a2cc-bf78c84e03c8"
},
"38211c3f-6c48-8987-0c698b9b9b52" : {
"email" : "c#gmail.com",
"gender" : "m",
"name" : "chirag",
"user_id" : "38211c3f-6c48-8987-0c698b9b9b52"
},
"a536cf1f-419f-9658-cfb5f5a8ec78" : {
"email" : "a#gmail.com",
"gender" : "m",
"name" : "aniket",
"user_id" : "a536cf1f-419f-9658-cfb5f5a8ec78"
},
"c80d828d-48d7-a79a-9cc558cd1960" : {
"email" : "b#gmail.com",
"gender" : "f",
"name" : "bhakti",
"user_id" : "c80d828d-48d7-a79a-9cc558cd1960"
}
}
}
Now I want to get 'name' and 'id' where 'gender' is "m"
Please can anyone tell me how to fire a query like this from Android client to firebase.
I tried tutorials provided by firebase and few other links but didn't got through.
Most of solutions are in Javascript I need it for Android in firebase's java api.
I tried this
public class ActivityHome extends Activity {
String strLoggedInUserID;
TextView tvData;
Firebase fbRef = new Firebase("https://myurl.com/users");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tvData = (TextView) findViewById(R.id.tvData);
Query queryRef = fbRef.orderByChild("gender").equalTo("m");
Bundle extras = getIntent().getExtras();
strLoggedInUserID = extras.getString("userID");
fbRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//long longChildCount = dataSnapshot.getChildrenCount();
// tvData.setText(dataSnapshot).toString();
tvData.setText(s);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//tvData.setText(s);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
Your querying code looks correct, but you're not printing the user.
Since you're adding a ChildEventListener, it will be called for each child:
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//long longChildCount = dataSnapshot.getChildrenCount();
// tvData.setText(dataSnapshot).toString();
tvData.setText(s);
}
The dataSnapshot parameter contains information about a single user. To print their key, name and email:
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println(dataSnapshot.key());
System.out.println(dataSnapshot.child("name").getValue());
System.out.println(dataSnapshot.child("email").getValue());
}
Alternatively, you can listen to all children at once with a ValueEventListener.
private static class User {
String email;
String gender;
String name;
#JsonProperty("user_id")
String userId;
public String getEmail() { return email; }
public String getGender() { return gender; }
public String getName() { return name; }
public String getUserId() { return userId; }
}
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
User user = child.getValue(User.class);
System.out.println(user.getEmail());
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
In this last snippet I also use a User class for getting the data from DataSnapshot. As your application grows, this will be more convenient than constantly calling DataSnapshot.child().
This is covered in the section on reading data in the Firebase documentation.

Categories

Resources