Dane B
Dane B

Reputation: 177

SharedPreferences putting in null values inside ValueEventListener

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.

Upvotes: 0

Views: 48

Answers (1)

navylover
navylover

Reputation: 13539

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

Upvotes: 1

Related Questions