I am implementing firebase in my app. I have a requirement where I have to change the key value of one object. Please refer the below image as a reference to my firebase database.
For the DCu1, I need to change its key value to something else, suppose DCu4. Now for that I am creating another cloned object of DCu1 with changed key value as DCu4 and after which I will delete DCu1. The issue is with the nested parameters like DeviceList, Status, Request. How can I copy these values to the new cloned object? need your help. Thanks in advance.
When you read data at a node, all the nested stuff are downloaded.
You can use the code below:
FirebaseDatabase.getInstance().getReference().child("/path/to/DCu1").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
FirebaseDatabase.getInstance().getReference().child("path/to/DCu4").setValue(dataSnapshot.getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I hope this helps
Related
I'm having trouble getting any values from my firebase database for android using the documentation that was given. I've tried connecting to the simplest structure possible but still no luck. Below is my code.
DatabaseReference usersReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
usersReference = FirebaseDatabase.getInstance().getReference("users");
...
}
protected void onStart() {
super.onStart();
System.out.println("User Reference: "+usersReference.toString());
usersReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.child("name").getValue(String.class);
mongo.setText(value);
}
#Override
public void onCancelled(DatabaseError error) {
}
});
}
Here is what my database looks like in firebase
For some reason no matter what I try I get nothing. I tried printing the user reference to make sure I was going to the correct place, and although the link was the correct reference to the database when I clicked the link the I was met with this screen:
Not sure if that is relevant or not. Thank you
From the rest of the behavior you're describing it seems like your code is looking at a different database from what's shown in the screenshot. I recommend re-downloading the google-services.json for your project, and adding it to the project again.
When that happens, you better use the debugger to understand what's going on. Are you using Android Studio right?
Put a breakpoint on the following line:
String value = dataSnapshot.child("name").getValue(String.class);
And then, when the program execution stops here, click the "evaluate expression" button.
Inside that window you can experiment with dataSnapshot. I suggest you to try dataSnapshot.getChildren() there and see what does it show.
I keep getting the old values (which no longer is in the database) from my Firebase database. Here is how the database looks right now:
I am getting the info under friendlist. It used to be only one child there with key-value set soosk: true, but now it looks like in the photo. When using addListenerForSingleValueEvent() to my databaseRef, the friendlist retrieved only has soosk: true in it. Here is my code:
mFirebaseDatabaseReference = FirebaseDatabase
.getInstance()
.getReference(
"users/"+FirebaseAuth.getInstance().getCurrentUser().getUid()
);
mFirebaseDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Log.d(TAG, user.getFriendlist().toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The method you need to use to have all the values updated is addValueEventListener(). Because you need to listen for more than one value this is the only way to achieve it and to use addListenerForSingleValueEvent().
The most important thing is to remove the listener in your onDestroy method like this:
yourReference.removeEventListener(valueEventListener);
i am trying to save new files in numbered order. so there is a count child it get updated but no new child is created
this is the code
databaseReference.child("AdminList").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
long key= (long) dataSnapshot.child("count").getValue();
databaseReference.child("AdminList").child("count").setValue(key+1);
databaseReference.child("AdminList").child(String.valueOf(key+1)).child("uid").setValue(uid);
databaseReference.child("AdminList").child(String.valueOf(key+1)).child("Aname").setValue(fname);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});``
You are trying to store your data in a "continuous" manner, which is something that JSON objects just aren't designed do. If you are looking for an ordered list, use the priorities property, or you can order the data on the client side. This means that you can just store the data, and then either order client-side, or prioritize the children. I would look to do something like:
databaseReference.child("AdminList").child("uid").setValue(uid);
Use push() to create new child nodes:
databaseReference.child("childName").push();
This my database in firebase
I want to get the value of page which is equal to 200.
When I tried like that:
DatabaseReference my= userid_database.child("Books").child("Book 1").child("Page");
System.out.println(my);
Output:
https://my-proje-book-timer.firebaseio.com/qpPLvgpM0TMF6vz4Kqw8QaYroG83/Books/Book%201/Page
When I tried like that:
DatabaseReference my= userid_database.child("Books").child("Book 1").child("Page").getKey();
System.out.println(my);
Output:
Page
I don't want to use this:
my.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Because I want to get all values for each Book pages and make a calculation with these values. In this way, I couldn't get the values outside of the method.
If you don't want to use a listener, you can use the REST API to easily make an HTTP query to find the data you want, assuming that it's readable without authentication. If you need authentication, it gets more difficult.
But you're really better off just learning now to make the listeners work for you, even if you're not accustomed to that kind of processing.
I have the following data structure on firebase for the user MF0qeRA4p7djfjgXxqwFOck3m6p02. I want to get the value of item3 to populate a single field into the User interface on an Android App. I have been looking through samples on Stackoverflow, but all I have found are outdated and do not work with the current version of firebase. I'm new to firebase completely and this is my first app on android. I've got the oncreate user method to populate the users email address and add the 4 item fields, but retrieving the data I'm completely lost and I am not sure where to even begin.
-Users
---MF0qeRA4p7djfjgXxqwFOck3m6p02
------item1:"1"
------item2:"2"
------item3:"3"
------item4:"4"
According to what I can identify is, you are facing problem retrieving data from this reference. Here is the code:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.child("MF0qeRA4p7djfjgXxqwFOck3m6p02").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Object> map=(Map<String, Object>)dataSnapshot.getValue();
String item3=(String)map.get("item3");
display(item3);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope this helps.
You can create a custom model and inside you can insert elements. Something like this:
public class Item {
private List<Object> ojects;
}
There you can save instance of Item on database. In this case you have more controll. Other case is to use push() method, that will generate a new encoded key, something like this:
mDatabase.child("items").push().put(new Object());