SharedPreferences putting in null values inside ValueEventListener












0















I am trying find the match of right node in my Firebase database and save the values to SharedPreferences. I iterate through each node in Firebase with a ValueEventListener. But when I try to print out the values I have just put into the SharedPreferences, it shows null for all fields.



I have already checked to see if there is an exception by checking the value of commit(), but it returns true.



I have also thought maybe it was the thread finishing its task at a later time, so I made a runnable inside the foreach loop and made synchronous tasks. The first one saved the values to SharedPreferences, the next printed the values. But that also did not work.



I also thought it may be a problem with inconsistent contexts but 'sharedpref' and 'editor' both use the same activity context. And I have had no problems accessing the data I want in other activities.



Here is where I iterate through my Firebase nodes:



final String name = et_name.getText().toString();
DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
rootref.child("drivers_" + sharedPref.getString("frat_code", null))
.orderByChild("first_name")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
boolean found = false;
iterateFirebase:
for(DataSnapshot node: dataSnapshot.getChildren()) {
Driver driver = node.getValue(Driver.class);
if((driver.getName() != null) && (driver.getName().equals(name))) {
found = true;
editor.putString(driver.getName(), "name");
editor.putString(driver.getOccupants(), "occupants");
editor.putString(driver.getPhone(), "phone");
editor.putString(driver.getMake(), "make");
editor.putBoolean("isLogged", true);
editor.apply();
Log.d(TAG, "Values retrieved from Firebase node: " +
driver.getName() + ", " + driver.getOccupants() +
", " + driver.getMake() + ", " +
driver.getPhone());
Log.i(TAG, "Following user logged in: " +
sharedPref.getString("name", null) + ", " +
sharedPref.getString("occupants", null) +
", " + sharedPref.getString("phone", null) +
", " + sharedPref.getString("make", null) +
", " + sharedPref.getBoolean("isLogged",
false));
Intent intent = new Intent(IfYes.this,
MainActivity.class);
IfYes.this.startActivity(intent);
break iterateFirebase;
}
}


Here are the logs for the above log statements:



Values retrieved from Firebase node: John Smith, 4, Honda Civic, Black, 
+11234567890
Following user logged in: null, null, null, null, true


Thanks in advance for the help. Also I am still a beginner so any code style critiques would be appreciated if anything stands out.










share|improve this question





























    0















    I am trying find the match of right node in my Firebase database and save the values to SharedPreferences. I iterate through each node in Firebase with a ValueEventListener. But when I try to print out the values I have just put into the SharedPreferences, it shows null for all fields.



    I have already checked to see if there is an exception by checking the value of commit(), but it returns true.



    I have also thought maybe it was the thread finishing its task at a later time, so I made a runnable inside the foreach loop and made synchronous tasks. The first one saved the values to SharedPreferences, the next printed the values. But that also did not work.



    I also thought it may be a problem with inconsistent contexts but 'sharedpref' and 'editor' both use the same activity context. And I have had no problems accessing the data I want in other activities.



    Here is where I iterate through my Firebase nodes:



    final String name = et_name.getText().toString();
    DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
    rootref.child("drivers_" + sharedPref.getString("frat_code", null))
    .orderByChild("first_name")
    .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    boolean found = false;
    iterateFirebase:
    for(DataSnapshot node: dataSnapshot.getChildren()) {
    Driver driver = node.getValue(Driver.class);
    if((driver.getName() != null) && (driver.getName().equals(name))) {
    found = true;
    editor.putString(driver.getName(), "name");
    editor.putString(driver.getOccupants(), "occupants");
    editor.putString(driver.getPhone(), "phone");
    editor.putString(driver.getMake(), "make");
    editor.putBoolean("isLogged", true);
    editor.apply();
    Log.d(TAG, "Values retrieved from Firebase node: " +
    driver.getName() + ", " + driver.getOccupants() +
    ", " + driver.getMake() + ", " +
    driver.getPhone());
    Log.i(TAG, "Following user logged in: " +
    sharedPref.getString("name", null) + ", " +
    sharedPref.getString("occupants", null) +
    ", " + sharedPref.getString("phone", null) +
    ", " + sharedPref.getString("make", null) +
    ", " + sharedPref.getBoolean("isLogged",
    false));
    Intent intent = new Intent(IfYes.this,
    MainActivity.class);
    IfYes.this.startActivity(intent);
    break iterateFirebase;
    }
    }


    Here are the logs for the above log statements:



    Values retrieved from Firebase node: John Smith, 4, Honda Civic, Black, 
    +11234567890
    Following user logged in: null, null, null, null, true


    Thanks in advance for the help. Also I am still a beginner so any code style critiques would be appreciated if anything stands out.










    share|improve this question



























      0












      0








      0








      I am trying find the match of right node in my Firebase database and save the values to SharedPreferences. I iterate through each node in Firebase with a ValueEventListener. But when I try to print out the values I have just put into the SharedPreferences, it shows null for all fields.



      I have already checked to see if there is an exception by checking the value of commit(), but it returns true.



      I have also thought maybe it was the thread finishing its task at a later time, so I made a runnable inside the foreach loop and made synchronous tasks. The first one saved the values to SharedPreferences, the next printed the values. But that also did not work.



      I also thought it may be a problem with inconsistent contexts but 'sharedpref' and 'editor' both use the same activity context. And I have had no problems accessing the data I want in other activities.



      Here is where I iterate through my Firebase nodes:



      final String name = et_name.getText().toString();
      DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
      rootref.child("drivers_" + sharedPref.getString("frat_code", null))
      .orderByChild("first_name")
      .addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      boolean found = false;
      iterateFirebase:
      for(DataSnapshot node: dataSnapshot.getChildren()) {
      Driver driver = node.getValue(Driver.class);
      if((driver.getName() != null) && (driver.getName().equals(name))) {
      found = true;
      editor.putString(driver.getName(), "name");
      editor.putString(driver.getOccupants(), "occupants");
      editor.putString(driver.getPhone(), "phone");
      editor.putString(driver.getMake(), "make");
      editor.putBoolean("isLogged", true);
      editor.apply();
      Log.d(TAG, "Values retrieved from Firebase node: " +
      driver.getName() + ", " + driver.getOccupants() +
      ", " + driver.getMake() + ", " +
      driver.getPhone());
      Log.i(TAG, "Following user logged in: " +
      sharedPref.getString("name", null) + ", " +
      sharedPref.getString("occupants", null) +
      ", " + sharedPref.getString("phone", null) +
      ", " + sharedPref.getString("make", null) +
      ", " + sharedPref.getBoolean("isLogged",
      false));
      Intent intent = new Intent(IfYes.this,
      MainActivity.class);
      IfYes.this.startActivity(intent);
      break iterateFirebase;
      }
      }


      Here are the logs for the above log statements:



      Values retrieved from Firebase node: John Smith, 4, Honda Civic, Black, 
      +11234567890
      Following user logged in: null, null, null, null, true


      Thanks in advance for the help. Also I am still a beginner so any code style critiques would be appreciated if anything stands out.










      share|improve this question
















      I am trying find the match of right node in my Firebase database and save the values to SharedPreferences. I iterate through each node in Firebase with a ValueEventListener. But when I try to print out the values I have just put into the SharedPreferences, it shows null for all fields.



      I have already checked to see if there is an exception by checking the value of commit(), but it returns true.



      I have also thought maybe it was the thread finishing its task at a later time, so I made a runnable inside the foreach loop and made synchronous tasks. The first one saved the values to SharedPreferences, the next printed the values. But that also did not work.



      I also thought it may be a problem with inconsistent contexts but 'sharedpref' and 'editor' both use the same activity context. And I have had no problems accessing the data I want in other activities.



      Here is where I iterate through my Firebase nodes:



      final String name = et_name.getText().toString();
      DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
      rootref.child("drivers_" + sharedPref.getString("frat_code", null))
      .orderByChild("first_name")
      .addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      boolean found = false;
      iterateFirebase:
      for(DataSnapshot node: dataSnapshot.getChildren()) {
      Driver driver = node.getValue(Driver.class);
      if((driver.getName() != null) && (driver.getName().equals(name))) {
      found = true;
      editor.putString(driver.getName(), "name");
      editor.putString(driver.getOccupants(), "occupants");
      editor.putString(driver.getPhone(), "phone");
      editor.putString(driver.getMake(), "make");
      editor.putBoolean("isLogged", true);
      editor.apply();
      Log.d(TAG, "Values retrieved from Firebase node: " +
      driver.getName() + ", " + driver.getOccupants() +
      ", " + driver.getMake() + ", " +
      driver.getPhone());
      Log.i(TAG, "Following user logged in: " +
      sharedPref.getString("name", null) + ", " +
      sharedPref.getString("occupants", null) +
      ", " + sharedPref.getString("phone", null) +
      ", " + sharedPref.getString("make", null) +
      ", " + sharedPref.getBoolean("isLogged",
      false));
      Intent intent = new Intent(IfYes.this,
      MainActivity.class);
      IfYes.this.startActivity(intent);
      break iterateFirebase;
      }
      }


      Here are the logs for the above log statements:



      Values retrieved from Firebase node: John Smith, 4, Honda Civic, Black, 
      +11234567890
      Following user logged in: null, null, null, null, true


      Thanks in advance for the help. Also I am still a beginner so any code style critiques would be appreciated if anything stands out.







      android firebase firebase-realtime-database sharedpreferences event-listener






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 11:35









      KENdi

      5,8392921




      5,8392921










      asked Nov 27 '18 at 20:58









      Dane BDane B

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Notice:



          editor.putString(driver.getName(), "name");
          editor.putString(driver.getOccupants(), "occupants");
          editor.putString(driver.getPhone(), "phone");
          editor.putString(driver.getMake(), "make");


          should be:



          editor.putString("name", driver.getName());
          editor.putString("occupants", driver.getOccupants());
          editor.putString("phone", driver.getPhone());
          editor.putString("make", driver.getMake());





          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',
            autoActivateHeartbeat: false,
            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%2f53508045%2fsharedpreferences-putting-in-null-values-inside-valueeventlistener%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Notice:



            editor.putString(driver.getName(), "name");
            editor.putString(driver.getOccupants(), "occupants");
            editor.putString(driver.getPhone(), "phone");
            editor.putString(driver.getMake(), "make");


            should be:



            editor.putString("name", driver.getName());
            editor.putString("occupants", driver.getOccupants());
            editor.putString("phone", driver.getPhone());
            editor.putString("make", driver.getMake());





            share|improve this answer




























              1














              Notice:



              editor.putString(driver.getName(), "name");
              editor.putString(driver.getOccupants(), "occupants");
              editor.putString(driver.getPhone(), "phone");
              editor.putString(driver.getMake(), "make");


              should be:



              editor.putString("name", driver.getName());
              editor.putString("occupants", driver.getOccupants());
              editor.putString("phone", driver.getPhone());
              editor.putString("make", driver.getMake());





              share|improve this answer


























                1












                1








                1







                Notice:



                editor.putString(driver.getName(), "name");
                editor.putString(driver.getOccupants(), "occupants");
                editor.putString(driver.getPhone(), "phone");
                editor.putString(driver.getMake(), "make");


                should be:



                editor.putString("name", driver.getName());
                editor.putString("occupants", driver.getOccupants());
                editor.putString("phone", driver.getPhone());
                editor.putString("make", driver.getMake());





                share|improve this answer













                Notice:



                editor.putString(driver.getName(), "name");
                editor.putString(driver.getOccupants(), "occupants");
                editor.putString(driver.getPhone(), "phone");
                editor.putString(driver.getMake(), "make");


                should be:



                editor.putString("name", driver.getName());
                editor.putString("occupants", driver.getOccupants());
                editor.putString("phone", driver.getPhone());
                editor.putString("make", driver.getMake());






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 21:22









                navylovernavylover

                3,51531119




                3,51531119
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508045%2fsharedpreferences-putting-in-null-values-inside-valueeventlistener%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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks