0

I am trying to use Sharedpreference in our program but it's give me default value, I am trying much more but not getting what I make mistake I am provide classes and xml file that's needed

package com.example.preferenceexample;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

  public class MainActivity extends Activity {
private static final int RESULT_SETTINGS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showUserSetting();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.settings, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()) {

    case R.id.menu_settings:
        Intent i = new Intent(this, UserSettingActivity.class);
        startActivityForResult(i, RESULT_SETTINGS);
        System.out.println("prefrence Activity called");
        break;

    }

    return true;
}

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case RESULT_SETTINGS:
            showUserSetting();
            break;

        }

    }
  private void showUserSetting()
{
    SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
    StringBuilder sb=new StringBuilder();
    sb.append("\n User Name : "+sp.getString("pref_Username",null ));
     sb.append("\n Send report: " + sp.getBoolean("prefSendReport", false));
     sb.append("\n Sync Frequency:  "+ sp.getString("prefSecFrequency",null));
     TextView settingsTextView=(TextView) findViewById(R.id.textView1);
     settingsTextView.setText(sb.toString());
}

 }




  public class UserSettingActivity extends PreferenceActivity {

  public void onCreate(Bundle bundle)
  {
    super.onCreate(bundle);
    addPreferencesFromResource(R.xml.settings);
  }

   }




    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 <PreferenceCategory android:title="@string/pref_user_profile">
 <EditTextPreference 
   android:title="@string/pref_user_name"
   android:summary="@string/pref_user_name_summary"
   android:key="pref_Username"/> 
 </PreferenceCategory>
 <PreferenceCategory android:title="@string/pref_update_setting">
  <CheckBoxPreference
      android:defaultValue="false"
      android:key="prefSendReport"
      android:summary="@string/pref_send_report_summary"
      android:title="@string/pref_send_report"></CheckBoxPreference>
  </PreferenceCategory>
 <PreferenceCategory>
  <ListPreference
      android:title="@string/pref_sync_frequency"
      android:key="prefSecFrequency"
      android:summary="@string/pref_sync_frequency_summary"
      android:entries="@array/syncFrequency"
      android:entryValues="@array/syncFrequencyValues"
      />
  </PreferenceCategory>
  </PreferenceScreen>
Cœur
  • 37,241
  • 25
  • 195
  • 267
shrawan
  • 11
  • 3

4 Answers4

0

You are giving "NULL" as a default value , which is a String object , instead your null object,dont use double quote , it makes it String constant.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
0

This snippit of code is just showing the revival of preferences. If you want to put the preferences use this:

sp.edit().putString("pref_Username", "Test").commit();
Daan Olislagers
  • 3,253
  • 2
  • 17
  • 35
0

May be your doing some thing wrong when your are saving them to device..

please have a look at the below sample methods and try..

public String getLastRostersDownloadedTime(String key) {
    SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
    String value = sp.getString(key, null);
    LogUtils.LOGE("Last Download Time ::", "" + value);
    return value;
}

public boolean saveLastRostersDownloadedTime(String key, String value) {
    SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
    Editor edit = sp.edit();
    edit.putString(key, value);
    LogUtils.LOGE("Updateding new Download Time ::", "" + value);
    return edit.commit();
}

where context is the activity context. so should call these methods inside activity. or pass activity context as argument to these methods..

Ramesh Bugatha
  • 1,162
  • 1
  • 11
  • 24
0

I think the below code works for you . Declare this on top of the below the class

public static final String PREFS_NAME = "MyPrefsFile";

next use the below code where ever you want to consume

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); firstStart = settings.getBoolean("firstStart", true);

M Karthik
  • 11
  • 1