How to insert and retrieve Android code on this Firebase structure?
up vote
0
down vote
favorite
I am from PHP and MySQL background and I haven't worked with JSON based DB like Firebase.
I am looking for sample code to insert data in firebase "Realtime database". I am already done with authentication stage.
java android firebase firebase-realtime-database
add a comment |
up vote
0
down vote
favorite
I am from PHP and MySQL background and I haven't worked with JSON based DB like Firebase.
I am looking for sample code to insert data in firebase "Realtime database". I am already done with authentication stage.
java android firebase firebase-realtime-database
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am from PHP and MySQL background and I haven't worked with JSON based DB like Firebase.
I am looking for sample code to insert data in firebase "Realtime database". I am already done with authentication stage.
java android firebase firebase-realtime-database
I am from PHP and MySQL background and I haven't worked with JSON based DB like Firebase.
I am looking for sample code to insert data in firebase "Realtime database". I am already done with authentication stage.
java android firebase firebase-realtime-database
java android firebase firebase-realtime-database
edited Nov 10 at 16:03
Mikhail Kholodkov
3,66252343
3,66252343
asked Nov 10 at 6:06
Panjab Web
225
225
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25
add a comment |
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
To insert some data in your Firebase Database, you have to set the reference to the node you want to insert the data to and then use setValue()
method.
Suppose you want to change age
of the admins
node, in your database in the question.
In code it looks something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child("admins");
ref.child("age").setValue(76);
The above code will replace 42 with 76, in your admins
node's age
child.
Read more about this here.
Getting data from Firebase Database is a bit more work, as you have to use listeners for that. There are 3 different event listeners at your disposal, that are valueEventListener
childEventListener
and singleValueEventListener
.
These three eventListeners
have different properties and you can use them as you like.
Suppose you want to retrieve age
of your admins
node from your database, then you may use a code like this to help. Note ref
in this code is same as in above code.
ref.addSingleValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
int age = dataSnapshot.child("age").getValue(String.class);
// this will store value of age from database to the variable age
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.d("TAG:", "Couldn't read data ", error.toException());
);
Read more about this here.
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use.child("users")
and just push data to Firebase just with the specific child name likeadmins
andmembers
.
– PradyumanDixit
Nov 22 at 5:47
add a comment |
up vote
0
down vote
Refer to this link, https://firebase.google.com/docs/database/android/start/
Write a message to the database
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Read a message from the database
// Read from the database
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
To insert some data in your Firebase Database, you have to set the reference to the node you want to insert the data to and then use setValue()
method.
Suppose you want to change age
of the admins
node, in your database in the question.
In code it looks something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child("admins");
ref.child("age").setValue(76);
The above code will replace 42 with 76, in your admins
node's age
child.
Read more about this here.
Getting data from Firebase Database is a bit more work, as you have to use listeners for that. There are 3 different event listeners at your disposal, that are valueEventListener
childEventListener
and singleValueEventListener
.
These three eventListeners
have different properties and you can use them as you like.
Suppose you want to retrieve age
of your admins
node from your database, then you may use a code like this to help. Note ref
in this code is same as in above code.
ref.addSingleValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
int age = dataSnapshot.child("age").getValue(String.class);
// this will store value of age from database to the variable age
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.d("TAG:", "Couldn't read data ", error.toException());
);
Read more about this here.
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use.child("users")
and just push data to Firebase just with the specific child name likeadmins
andmembers
.
– PradyumanDixit
Nov 22 at 5:47
add a comment |
up vote
2
down vote
To insert some data in your Firebase Database, you have to set the reference to the node you want to insert the data to and then use setValue()
method.
Suppose you want to change age
of the admins
node, in your database in the question.
In code it looks something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child("admins");
ref.child("age").setValue(76);
The above code will replace 42 with 76, in your admins
node's age
child.
Read more about this here.
Getting data from Firebase Database is a bit more work, as you have to use listeners for that. There are 3 different event listeners at your disposal, that are valueEventListener
childEventListener
and singleValueEventListener
.
These three eventListeners
have different properties and you can use them as you like.
Suppose you want to retrieve age
of your admins
node from your database, then you may use a code like this to help. Note ref
in this code is same as in above code.
ref.addSingleValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
int age = dataSnapshot.child("age").getValue(String.class);
// this will store value of age from database to the variable age
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.d("TAG:", "Couldn't read data ", error.toException());
);
Read more about this here.
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use.child("users")
and just push data to Firebase just with the specific child name likeadmins
andmembers
.
– PradyumanDixit
Nov 22 at 5:47
add a comment |
up vote
2
down vote
up vote
2
down vote
To insert some data in your Firebase Database, you have to set the reference to the node you want to insert the data to and then use setValue()
method.
Suppose you want to change age
of the admins
node, in your database in the question.
In code it looks something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child("admins");
ref.child("age").setValue(76);
The above code will replace 42 with 76, in your admins
node's age
child.
Read more about this here.
Getting data from Firebase Database is a bit more work, as you have to use listeners for that. There are 3 different event listeners at your disposal, that are valueEventListener
childEventListener
and singleValueEventListener
.
These three eventListeners
have different properties and you can use them as you like.
Suppose you want to retrieve age
of your admins
node from your database, then you may use a code like this to help. Note ref
in this code is same as in above code.
ref.addSingleValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
int age = dataSnapshot.child("age").getValue(String.class);
// this will store value of age from database to the variable age
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.d("TAG:", "Couldn't read data ", error.toException());
);
Read more about this here.
To insert some data in your Firebase Database, you have to set the reference to the node you want to insert the data to and then use setValue()
method.
Suppose you want to change age
of the admins
node, in your database in the question.
In code it looks something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child("admins");
ref.child("age").setValue(76);
The above code will replace 42 with 76, in your admins
node's age
child.
Read more about this here.
Getting data from Firebase Database is a bit more work, as you have to use listeners for that. There are 3 different event listeners at your disposal, that are valueEventListener
childEventListener
and singleValueEventListener
.
These three eventListeners
have different properties and you can use them as you like.
Suppose you want to retrieve age
of your admins
node from your database, then you may use a code like this to help. Note ref
in this code is same as in above code.
ref.addSingleValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
int age = dataSnapshot.child("age").getValue(String.class);
// this will store value of age from database to the variable age
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.d("TAG:", "Couldn't read data ", error.toException());
);
Read more about this here.
edited Nov 10 at 6:25
answered Nov 10 at 6:19
PradyumanDixit
2,4712818
2,4712818
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use.child("users")
and just push data to Firebase just with the specific child name likeadmins
andmembers
.
– PradyumanDixit
Nov 22 at 5:47
add a comment |
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use.child("users")
and just push data to Firebase just with the specific child name likeadmins
andmembers
.
– PradyumanDixit
Nov 22 at 5:47
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
What if I don't use user at first level? how to register users within first level separately admins, members and helpers while using createUserWithEmailAndPassword mydb | admin | .... id: xast3241fdpa | ... name : john
– Panjab Web
Nov 22 at 5:45
Then just don't use
.child("users")
and just push data to Firebase just with the specific child name like admins
and members
.– PradyumanDixit
Nov 22 at 5:47
Then just don't use
.child("users")
and just push data to Firebase just with the specific child name like admins
and members
.– PradyumanDixit
Nov 22 at 5:47
add a comment |
up vote
0
down vote
Refer to this link, https://firebase.google.com/docs/database/android/start/
Write a message to the database
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Read a message from the database
// Read from the database
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
);
add a comment |
up vote
0
down vote
Refer to this link, https://firebase.google.com/docs/database/android/start/
Write a message to the database
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Read a message from the database
// Read from the database
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
);
add a comment |
up vote
0
down vote
up vote
0
down vote
Refer to this link, https://firebase.google.com/docs/database/android/start/
Write a message to the database
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Read a message from the database
// Read from the database
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
);
Refer to this link, https://firebase.google.com/docs/database/android/start/
Write a message to the database
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Read a message from the database
// Read from the database
myRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
@Override
public void onCancelled(DatabaseError error)
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
);
answered Nov 10 at 6:13
Zuhrain
281116
281116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236438%2fhow-to-insert-and-retrieve-android-code-on-this-firebase-structure%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hey @PanjabWeb do vote mark the answer as correct by clicking on V type tick mark looking button next to the answer, this helps the future stack overflow readers with similar questions and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 26 at 7:25