I'm working with firebase in a android application and something strange ocurred, in my example I need two snapshots, 1 to get the users inside a list ( I use this snapshot to fill a arraylist of strings with the key of the user) and the other to compare to the users, the strange behavior is that my arraylist is empty after the first snapshot, I used logcat to check it and that Log inside the firstsnapshot returns me 1 as the size of the arraylist, the second returns me 0, dunno how it gets 0 again.
Here is my code:
private void prepareFriendList() {
myRef.child(id).child("FriendLists").child(group).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String keyUser;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.d("number:",snapshot.getKey());
keyUser = snapshot.getKey();
currentFriends.add(keyUser);
Log.d("hello",String.valueOf(currentFriends.size()));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
for(String s: currentFriends){
Log.d("idddd",s);
}
Log.d("hello",String.valueOf(currentFriends.size()));
myRef.child(id).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
for(String item: currentFriends){
if (snapshot.getKey().equals(item)) {
usersList.add(user);
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mAdapter.notifyDataSetChanged();
}
I don't understand why that happens, since I'm adding the key inside the arraylist, any tip?
This is happening because onDataChange is called asynchronously. This means that the statement that adds users to the list is executed before onDataChange has been called. That's why your list is empty outside that method. So in order to use that lists, you need to use it inside the onDataChange() method.
For other approach, please visit this post and this post.
Hope it helps.
Related
MY CURRENT CODES (now its working)
#Override
protected void onStart() {
super.onStart();
petshopRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
petshopsList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Petshops petshops = postSnapshot.getValue(Petshops.class);
petshopsList.add(petshops);
}
CustomerPetshopAdapter adapter = new CustomerPetshopAdapter(CustomerViewPetshopActivity.this, R.layout.customlist_viewpetshop, petshopsList);
lvPetshops.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
QUESTION:
Lets say i have
petshopRef = FirebaseDatabase.getInstance().getReference("Petshops");
How do I retrieve all data in Blue Circle (in my image above)
You can retrieve all the data using the once value event. Then you can loop through it to get each key and value. I'm writing this logic in nodejs, you can convert it into your java logic.
Example,
petshopRef.once('value', function(snap){
snap.forEach(listData=>{ //list data will have top level auto generated keys
listData.forEach(element=>{
element.key // will be the key, in your case (brcat)
element.val() // will be the corresponding value to above key. in your case (3)
});
});
});
I could not figure out how to point my value listener to every child's last node as shown in this picture.
I think I need to use childevent listener but I don't know how to redirect it correctly.
Query searchItemQuery = userDatabaseReference.child(user.getUid());
searchItemQuery.addChildEventListener(new ChildEventListener(){
#Override
public onChildAdded, onChildChanged, onChildRemoved, onChildMoved, onCancelled
});
I would suggest that down for each item0001, item0002, instead of using firebase push id, you can store the unix timestamp in millis. That way you can easily sort your entries and get last child node for each item.
So, with this change your database would look like
root
user
uid
item0001
15089344450000 // this is a timestamp
itemdepositdate
itemwithdrawdate
15989922000000 // another timestamp
itemdepositdate
itemwithdrawdate
item0002
AND SO ON...
Now, for each item, you can point to last child simply by using below code.
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference()
.child("root")
.child("user")
.child("user_id_of_user")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
snapshot.getRef().orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getValue(); // this is the last itemdepositdate and itemwithdrawaldate for this item
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Query searchItemQuery = userDatabaseReference
.child(user.getUid)
.orderByKey() //order nodes w.r.t. the keys
.limitToLast(1); //gets last item
I have this scenario where in my app i am trying to query the child nodes and pass it on the list to recyclerview adapter and here comes the problem when i am scrolling up the recycler view items and if some one has inserted a post, my recyclerview is again coming to first post item and also i am using the viewpager with three fragments and whatever fragment i am on I am rolling back to the first fragment if some one has inserted the post how to solve this.
I have implemented this in following way mentioned below.
one way im thinking is i thought i would not listen to the childevent changes instead i would query the results and populate recyclerview later not listening to child events so that way everything states as it is and i dont know in firebase how do you retrieve values without implementing listeners I tried the singleValueEventListener that way still the behavior is same rolling back to first item or first fragment
guide me through solution how to get rid of this behavior.
Query query= databasePostsReference.orderByChild("timestamp");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<UserPostPOJO> listposts = new ArrayList<UserPostPOJO>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: entered list adding");
UserPostPOJO post =
snapshot.getValue(UserPostPOJO.class);
listposts.add(0,post);
}
if(listposts.isEmpty()){
empty.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
else
{
empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
makelist(listposts);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void makelist(List<UserPostPOJO> listposts) {
list = listposts;
Log.d(TAG,"size is "+ list.size()+"");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
CustomRecyclerViewAdapter adapter = new
CustomRecyclerViewAdapter(getActivity(), list,"recentfragment");
recyclerView.setAdapter(adapter);
}
There is a method called removeEventListener() that you can call to remove a specific event listener. You get data out from your database and than call this method. So in order to make this work, please use the following code:
databaseReference.removeEventListener(valueEventListener);
In which databaseReference is the reference where you intially put the listener.
For more details please read the offcial doc.
Hope it helps.
Firebase has a FirebaseRecyclerAdapter which can be set to a RecyclerView. It takes a DatabaseReference or Query object and handles all the data synchronization between your database and your view.
For a database reference object, the same way one can add an event listener, it can also be removed, using removeEventListener.
Instead of creating an anonymous object like this
Query query= databasePostsReference.orderByChild("timestamp");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<UserPostPOJO> listposts = new ArrayList<UserPostPOJO>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: entered list adding");
UserPostPOJO post =
snapshot.getValue(UserPostPOJO.class);
listposts.add(0,post);
}
if(listposts.isEmpty()){
empty.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
else
{
empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
makelist(listposts);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
you can create a named object of ValueEventListener and remove it from the database reference object using removeEventListener, at the end of the onDataChange method
Query query= databasePostsReference.orderByChild("timestamp");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<UserPostPOJO> listposts = new ArrayList<UserPostPOJO>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: entered list adding");
UserPostPOJO post =
snapshot.getValue(UserPostPOJO.class);
listposts.add(0,post);
}
if(listposts.isEmpty()){
empty.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
else
{
empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
makelist(listposts);
}
query.removeEventListener(valueEventListener);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
query.addValueEventListener(valueEventListener);
The code inside onDataChange method gets executed only once as the ValueEventListener object is removed as soon as the last line of the method gets executed.
I have this data in Firebase Database:
Negocios:
-KjfCu56lFZCybYldBZy
lugarReference: "Restaurante"
numeroMesas:39
password:"pass123"
user:"user123"
user_id:"usr_1"
I have two applications connected to one project in Firebase, one of them upload data of restaurants reservation, and the other one displays reservations in a RecyclerView.
In the first application, once I do a reservation, it must minus one to numeroMesas value, the other application should display any changes to this key.
In the reservation application, I have this piece of code that I did base in this question:
Update specific keys using Firebase for Android
It supposes to modify the value of numeroMesas, but it does not:
mReference.child("Negocios").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Lugar lugar = postSnapshot.getValue(Lugar.class);
if(nombre_lugar.equals(lugar.getUser_id())){
DatabaseReference numMesasReference = mReference.child(dataSnapshot.getKey()).child("numeroMesas");
numMesasReference.setValue(lugar.getNumeroMesas() - 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
What it does is the next thing:
Negocios:
-KjfCu56lFZCybYldBZy
lugarReference: "Restaurante"
numeroMesas:39
password:"pass123"
user:"user123"
user_id:"usr_1"
-Negocios
-KjfCu56lFZCybYldBZy
numeroMesas:39
How can be fixed?
I need to minus one once I do a reservation...
Greetings!
Using postSnapshot.getRef() you obtain a reference to the source location for this snapshot. So what's next is to add a child to that reference, that reference will be "numeroMesas" that is the key you want to modify.
As easy as that, the code will be like this.
mReference.child("Negocios").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Lugar lugar = postSnapshot.getValue(Lugar.class);
if(nombre_lugar.equals(lugar.getUser_id())){
DatabaseReference numMesasReference = postSnapshot.getRef().child("numeroMesas");
numMesasReference.setValue(lugar.getNumeroMesas() - 1);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
P.S: If you want to minus one just once by reservation, addValueEventListener() won't work, it will minus multiple time as a loop. Use addListenerForSingleValueEvent() instead addValueEventListener().
As the name says, this listener fits for single changes in the data at any location.
I guess the problem is with dataSnapshot.getKey() in this line
DatabaseReference numMesasReference = mReference.child(dataSnapshot.getKey()).child("numeroMesas");
Here you getting the key for the whole list of Negocios, not for the specific child replace it with `
postSnapshot.getKey()
The problem is that your DatabaseReference that you are using is wrong. You are missing a child. So in order to solve this problem, please use the following code:
mReference.child("Negocios").child(negociosId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int numeroMesas = (int) dataSnapshot.child("numeroMesas").getValue();
if(nombre_lugar.equals(lugar.getUser_id())){
mReference.child("Negocios").child(negociosId).child("numeroMesas").setValue(numeroMesas - 1);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
In which negociosId is the unique id generated by the push() method.
Hope it helps.
I'm new to firebase and android. But If the above solutions doesn't work please do the following. I think the problem is that you didn't convert the value of numeroMesas to string. Sorry if there's anything wrong.
Hope it helps.
mReference.child("Negocios").child(negociosId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(nombre_lugar.equals(lugar.getUser_id())){
mReference.child("Negocios").child(negociosId).child("numeroMesas").setValue((Integer.parseInt(dataSnapshot.child("numeroMesas").getValue().toString())) - 1);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
i am trying to get that push key underline red with the reference of child name underline blue but my code returning products as key on dataSnapshot.getKey here is code :
Code
Query reference=FirebaseDatabase.getInstance().getReference().child("products").orderByChild("name").equalTo("iphon");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String key=dataSnapshot.getKey();
Log.e(TAG,key);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
04-30 18:27:11.660 22742-22742/com.example.zar.shopistant E/MainActivity: products
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.
So to get the snapshot of the individual child node, you need to loop over the result:
Query reference=FirebaseDatabase.getInstance().getReference().child("products").orderByChild("name").equalTo("iphon");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String key=childSnapshot.getKey();
Log.i(TAG,key);
}
}