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.
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());
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View