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.



enter image description here










share|improve this question























  • 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














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.



enter image description here










share|improve this question























  • 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












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.



enter image description here










share|improve this question















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.



enter image description here







java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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.






share|improve this answer






















  • 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

















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());

);





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer






















    • 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














    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.






    share|improve this answer






















    • 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












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 like admins and members.
      – 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











    • 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















    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












    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());

    );





    share|improve this answer
























      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());

      );





      share|improve this answer






















        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());

        );





        share|improve this answer












        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());

        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 6:13









        Zuhrain

        281116




        281116



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo