I have list of contacts in Map object i want to compare each contact into Firebase and retrieve that node if contact matched as value.
Ex: Suppose i have 123,345,567 as 3 contacts i want to get that complete node if contact inside node.
Firebase Structure
-Users
-someId1
-contact:123
-fname:something
-someId2
-contact:345
-fname:something
-someId3
-contact:567
-fname:something
-someId4
-contact:980
-fname:something
How do i retrieve those complete nodes if given contact matched into Firebase node.
I have written something like this
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference reference=rootRef.child("Users");
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (Map.Entry<String, String> singleContact : contacts.entrySet()) {
query=reference.orderByChild("contact").equalTo(singleContact.getKey());
if (dataSnapshot.hasChild(singleContact.getKey()))
userModelObjects.add(dataSnapshot.child(singleContact.getKey()).getValue(FirebaseUserModel.class));
}
}
I am assuming the keys in your map are like the keys in your database:
//your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
//make a listener
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//this loop will extract all the values in every random ID
for(DataSnapshot ds : dataSnapshot.getChildren()){
//extract the values
String contact = ds.child("contact").getValue(String.class);
String fname = ds.child("fname").getValue(String.class);
//check if they exist in the map, and see what you can do.........
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//error getting data
}
};
ref.addValueEventListener(listener);
Related
As shown in the image below, I have a database "table" called fridge that has a child called food. food is an array that can contain one or more elements.
I want to access the last node and fetch the food elements and add them in a list, but I couldn't figure out how to do it.
Thank you for your help
You could follow the docs and use the limitToLast() method. Keys in firebase are ordered alphabetically.
You can try this to get the data:
mDatabase.child("fridge").child(fridgeId).child("food").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String foodItem = postSnapshot.getValue();
foodList.add(foodItem);
}
}
});
First, create a list to store those values.
List<String> food = new ArrayList<>();
Retrieve the last stored food inside each fridge key
mDatabase.child("fridge").child(yourPushID).child("food").limitToLast(1).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
String lastFood = snapshot.getValue(String.class);
//Add your food to the list
food.add(lastFood);
Log.e("Foods found:",""+lastFood);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Where yourPushID should be how you are generating those random keys to store your food
Where mDatabase is
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
I want to access article information but the problem I face is Article array names are some random keys and not like the one given here.
To access the keys inside the Article node, do the following:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Branch").child("Users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String key=datas.child("keys").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here the datasnapshot is Users you then iterate inside the random keys and get the values(keys) that are inside the Articles.
I need to update few nodes in firebase data which is posted from the server end.Need to update the node "is_done" to 0/1 from the device end.I have tried with different solutions but all became futile i.e it is adding a different node outside the "schedule" node.
Code snippet I have tried
private void updatemultiplefirebasedata() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference();
Query query = reference.child("schedule").child("22-12-2017").child("route").child("1").child("kid").child("21");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for(DataSnapshot d : dataSnapshot.getChildren()) {
Log.d("Keys",String.valueOf(d.getKey())); //returning all the keys
HashMap<String, Object> result = new HashMap<>();
result.put("is_done", "0");
reference.child(String.valueOf(d.getKey())).updateChildren(result); //update according to keys
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
That's because your database reference still points to the root of your tree. You should assign the desired path to that reference.
Also: You don't need Queries in order to access data directly. You can simply attach a listener to the Database Reference.
private void updatemultiplefirebasedata() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference().child("schedule").child("22-12-2017").child("route").child("1").child("kid").child("21");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for(DataSnapshot d : dataSnapshot.getChildren()) {
Log.d("Keys",String.valueOf(d.getKey())); //returning all the keys
HashMap<String, Object> result = new HashMap<>();
result.put("is_done", "0");
reference.child(String.valueOf(d.getKey())).updateChildren(result); //update according to keys
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Try this :
reference.child(d.getKey()).updateChildren(result);
remove String.valueOf from child because your key is integer and you are passing it as string so instead of pointing it to same child it will create new key with String "1"
i want to retrieve the rates in the firebase database and store it in an String, can anyone suggest me how to take the values alone from the database. i used this code and cannot get any values.i have added my database image for reference
DatabaseReference rootRef = FirebaseDatabase.getInstance().
getReference("fish-market-ukkadam");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String rates = ds.getValue(String.class);
Log.d("TAG", rates);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
rootRef.addListenerForSingleValueEvent(eventListener);
MY DATABASE IMAGE
Replace this:
FirebaseDatabase.getInstance().getReference("fish-market-ukkadam");
With this:
FirebaseDatabase.getInstance().getReference();
Your database project name is "fish-market-ukkadam", but that is not the name of the root node that you can access by name.
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
mValueView = (TextView) findViewById(R.id.textView);
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
com.google.firebase.database.Query query = mRef.child("Users").orderByChild("title").equalTo(name);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Map<String,Object> map = (Map<String, Object>) dataSnapshot.getValue();
//String Title = (String) map.get("title");
String Title = dataSnapshot.child("title").getValue().toString();
mValueView.setText(Title);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to show object title is same name value.
This is the Firebase database:
If you want to search title value only one time, without listening for updates, you can simply use :
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}});
Then if you get a reference to 'Users' you can do some logic with iteration, for example :
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String title = (String) singleSnapshot.child("title").getValue();
//do your logic
}
There are two problems in your code:
you're specifying the child node Users twice
a query results a list of results, which your onDataChange doesn't handle
specifying the child node Users twice
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
// ^^^^^
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
// ^^^^^
Easily fixed:
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/");
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
I'm not sure why you use getReferenceFromUrl() to begin with. For most applications, this accomplishes the same is simpler:
mRef = FirebaseDatabase.getInstance().getReference();
a query results a list of results
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
System.out.println(snapshot.child("title").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});