jakeinmn
jakeinmn

Reputation: 167

Android sharedPreferences is not saving or getting strings properly

edit: It got solved by user Guardanis 's comment elsewhere. Android SharedPreferences not loading and saving properly

The problem was that I never SAVED IT. I had a save function, but never saved it to preferences.

I've been getting null returns from getting strings from my saved preferences. I'm not sure how savedpreferences worked but my understanding was that when call a sharedpreferences, it creates the keypair file on the phone so you can come back to it later.

My program is essentially a string creation application. When you press a button, it creates a string to send as an sms. My settings activity page has four edittexts that save whatever is inside them with a buttonclick and returns to the main activity. The final button creates a String by getting the value from the keyvalue pair and constructs the message. However, I've always gotten null for each of the values.

Heres the code for the settings page and then the main page. Please ask if I could add more, I didn't add ALL of the code, just the sharedpreferences portions.

public SharedPreferences sp;
public Editor e;

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();


      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settingmenu);

    //SP
    sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
    // named preferences - get the default ones and finish with it
    e = sp.edit();

    Button tt = (Button)findViewById(R.id.savebutton);
    tt.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {
                finish();

        }
    });


public void save(View view)
{
    //THINGS HAPPEN HERE WITH SHARED PREFERENCES :(
    savethethings();

    this.finish();
    return;
}

public String finishedtext(String userstring)
{

    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    if(smsintroduction.isEmpty()) 
    {
        if(smsbody.isEmpty())
        {
            if(checkboxtext.isEmpty())

            {
                if(checkboxmessage.isEmpty()) //topkek for most AND statements Ive ever put in in if/then form
                {
                    //Essentially the DEFAULT if they're ALL null
                    smsbody = "Hi "+ userstring +"! This is coming from jake's phone and it wants to send a text so we can talk or whatever. ";
                }
            }
        }
    }




    Toast.makeText( this, "Creating text, then press send!", Toast.LENGTH_LONG).show();

    String thetext = "";

    thetext = smsintroduction + " " + smsbody + " " + checkboxtext;

    return thetext;
}

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();

  //      if(!introstring.isEmpty()) //if the fields are NOT empty, they should get saved.
  //      {
      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

Upvotes: 0

Views: 165

Answers (3)

kevz
kevz

Reputation: 2737

change this

sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

to

sp = getPreferences(0);

Upvotes: 1

Zusee Weekin
Zusee Weekin

Reputation: 1358

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.

You can save your four texts as follow:

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
shared.edit().putString("text_name1", "value1").commit();

Then in activity(where you need to get them back)

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
String text_name1=shared.getString("text_name1", null);

Upvotes: 1

Anjali Tripathi
Anjali Tripathi

Reputation: 1477

try this.

//declareation
    private SharedPreferences _sPrefs=null;
    //do this in oncreate
    _sPrefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    //this is for your editor 
    SharedPreferences.Editor prefEditor = _sPrefs.edit();

Upvotes: 1

Related Questions