jerry fa
jerry fa

Reputation: 29

retrieving firebase data and saving it to shared preferences produces null

I have a database on firebase from that I'd like to retrieve some data and save it to my app using shared preferences. When I try to get the value in another activtiy from shared preferences I saved before I get null. BUT the toast gets popped up with the right values.

I call getLocDbs() from onCreate().

I tried async task but I read after that Firebase is async itself.

Why do I get null?

Thanks

 public void getLocDbs() {

    firebaseRef.child("rpi_lox").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                Toast.makeText(Welcomer.this, singleSnapshot.getValue
                        (postObject.class).result, Toast.LENGTH_SHORT).show();
                allLox += singleSnapshot.getValue
                        (rpiLocTemplate.class).locName+ ";";

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    PreferenceManager.getDefaultSharedPreferences(Welcomer.this)
            .edit()
            .putString("ls_locations", allLox).apply();


}

Upvotes: 0

Views: 1277

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

You need to move

PreferenceManager.getDefaultSharedPreferences(Welcomer.this)
        .edit()
        .putString("ls_locations", allLox).apply();

inside onDataChange because the network call will take some time to finish and onDataChange will be called when response is received otherwise you are saving null in preference and data is being received later

 public void getLocDbs() {

    firebaseRef.child("rpi_lox").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                Toast.makeText(Welcomer.this, singleSnapshot.getValue
                        (postObject.class).result, Toast.LENGTH_SHORT).show();
                allLox += singleSnapshot.getValue
                        (rpiLocTemplate.class).locName+ ";";

            }
            PreferenceManager.getDefaultSharedPreferences(Welcomer.this)
            .edit()
            .putString("ls_locations", allLox).apply();
       }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

Do you have any idea, why my string begins with null?

you have declare string as String allLox; which will assign null value and when you do += then it will append null in first iteration as

`allLox = allLox + otherstuff`
 // allLox = null + otherstuff

so declare your string variable as

String allLox = "";

or you can use StringBuilder while using append method to achieve efficiency

Upvotes: 1

Related Questions