I have a simple question about firebase
I'm creating an app which adding some data to the firebase,
when the user clicks on the button, some data should be added to firebase
How can I do this by coding ?
First, you need to create a model with (firstname,lastname,phone), and then get reference to the database as shown below.
MainData mainData =new MainData("ABC","XYZ","789798");
DatabaseReference mRefrence = mDatabase.child("users").getRef().push();
mRefrence.setValue(mainData);
By using push with a reference you can add values to the firebase database.
Related
I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.
I am new to Android programming. I am working on an app. When I signup in my app this shows the previously added data and does not ask me to add new user data to the database.
Even if I again login with a registered user then it again shows the previously added data.
I want that when I create a new user then a new node should create and then its child with data.
Database Picture
First of all, you need unique id to share your data in firebase. You can use user id for that. If you use firebase authentication. you can get user id from
FirebaseAuth.getInstance().getCurrentUser().getUid();
You can use this with email password firebase authentication. Likewise, you can get a unique id for authentication. Then u need to use that id as a key of your firebase database. Then your data structure should be changed like below. (key would be userids)
childsQuerity
|-------------adminInfo
|-------key1 -> value1
|-------asas1234 -> value2
|-------------childInfo
|-------key1 -> value1
|-------key2 -> value2
in your android activity use below command to push data to db
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("childInfo").child(userid);
ref.setValue(userDataUWant);
When creating a new user, get userid and update that firebase data like above-mentioned method. Then if an already registered user logged to the system. u can get user id using above mentioned method and you can get that user details as well.
I am implementing Firebase Realtime Database for my app.
The database contains a lot of entries that will continue to grow in the future.
I want to retrieve only a specific child from the database, but i can't find any implementation for doing that.
In Firebase too the implementation retrieves all the entries from the database.
This will be cumbersome in the future.
Is there any way to retrieve only specific entries or just one entry(depending upon the customisation)?
Please help.
#ALex Mamo: This is the database image
Perhaps you are refering to shallow query, which only read the first child ? In this case you might want to look at the API implementation as mentioned here:
Firebase (2016) Shallow Query
There are two different ways of getting it. You'll have to change 'messages' to the name of your database. Replace PostTitle with the specific data you're trying to receive.
var rootRef = firebase.database().ref().child("messages");
rootRef.on("child_added", snap=> {
var postTitle = snap.child("PostTitle").val();
or
exports.sendPushNotification = functions.database.ref('/messages/{id}').onWrite((change, context) => {
const afterData = change.after.val();
const postTitle = afterData.PostTitle;
if you want retrieve few records this might help. as per image structure attached.
you can restructure the data, and use set method with keys as 0,1,2.... instead of "LK34h...."->0 .
use set method and store latest index value.
you retrive like this
firebase.database().ref().child('users_Post/0').on('value',sn=>{ console.log(sn.val())//use sn.val() })
I am using Firebase realtime DB in my app,I could not use multiple order by in my query.If i am not use that one,looping the data again and again.It leads my app run slow.So please help me how to do that.Otherwise how to get multiple items in single Query
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("issue").orderByChild("id").equalTo(0,1,3);
...Like,is it possible????
I am making an Android app with Firebase and I want to add sections to each FirebaseUser (like wins and losses) but I don't think you can add to those objects. Should I just use the Realtime Database to do this or is there a better way?
Thanks in advance!
DatabaseReference ref1= FirebaseDatabase.getInstance().getReference();
DatabaseReference ref2,ref3;
ref2= ref1.child(user);//user is the name of user
ref2.child("Wins").setValue(""what he won);
ref2.child("Loose").setValue(""what he Lose);
Here I created a child to root node with user name, added sub children(Win & Lose) to user and then I set values to these children.
And you can retrieve that data too based on your query.
Refer following link once.
https://firebase.google.com/docs/database/android/start/