Thank for the reply guys, regarding the link that where I got this
SharedPreferences sp = this.getActivity().**getSharedPreferences("**
CHECKBOX", 0);

I've been going over the documentation trying to figure it out
Saving Key-Value Sets | Android
Developers<http://developer.android.com/training/basics/data-storage/shared-preferences.html#ReadSharedPreference>

Write

SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

Read

SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore =
sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

Im used to save like this..

private void savePrefs(String key, boolean value) {

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

Editor edit = sp.edit();

edit.putBoolean(key, value);

edit.commit();

  }

Anyways, thnx for the help..

On Thu, Feb 21, 2013 at 11:37 PM, Tamilarasi Sivaraj <javapo...@gmail.com>wrote:

> Hi,
> The following link is got the Android SharedPreferences in Fragment
>
> http://stackoverflow.com/questions/11741270/android-sharedpreferences-in-fragment
>
> Regards
> Android developer
> Trinay Technology Solutions
> www.trinaytech.com
> 5705750475
>
>
> On Friday, February 22, 2013 8:09:39 AM UTC+5:30, Marcelo Corvezola wrote:
>
>> Hmm thnx..I must be doing something else wrong then..cause when I use
>> this code
>> private void loadPrefs() {
>>
>>     ///SharedPreferences sp = this.getActivity().**getSharedPreferences("
>> **CHECKBOX", 0);
>>
>>     SharedPreferences sp = PreferenceManager.getDefaultSh**
>> aredPreferences(this);
>>
>> boolean cbValue = sp.getBoolean("CHECKBOX", false);
>>
>> String name = sp.getString("NAME", "");
>>
>> if(cbValue){
>>
>> cb.setChecked(true);
>>
>>  }else{
>>
>> cb.setChecked(false);
>>
>> }
>>
>> et.setText(name);
>>
>> }
>>
>> }
>>
>>
>> I get this error "The method getDefaultSharedPreferences(**Context) in
>> the type PreferenceManager is not applicable for the arguments
>>
>>  (TestFragment2)"
>>
>>
>> I thought had to replace this
>> SharedPreferences sp = PreferenceManager.getDefaultSh**aredPreferences(
>> this);
>>
>> With this
>> SharedPreferences sp = this.getActivity().**getSharedPreferences("**
>> CHECKBOX", 0);
>>
>>
>> and I get no errors, however this Throws a NullPointer Exception at
>> loadPrefs();
>>
>>
>> public final class TestFragment2 extends Fragment {
>>      TextView et;
>>
>>     TextView text;
>>
>>     SharedPreferences sp;
>>
>>     View myView;
>>
>>     CheckBox cb;
>>
>>
>>     @Override
>>
>>     public void onCreate(Bundle savedInstanceState) {
>>
>>         super.onCreate(**savedInstanceState);
>>
>>         loadPrefs();
>> }
>>
>>
>>     @Override
>>
>>     public View onCreateView(LayoutInflater inflater, ViewGroup
>> container, Bundle savedInstanceState) {
>>
>>         View myView = inflater.inflate(R.layout.frag**ment_2, container,
>> false);
>>
>>         text = (TextView)myView.findViewById(**R.id.textView2);
>>
>>         et = (TextView)myView.findViewById(**R.id.textView3);
>>         return myView;
>>
>>
>> }
>>
>>
>>        private void loadPrefs() {
>>
>>        SharedPreferences sp = this.getActivity().**getSharedPreferences("
>> **CHECKBOX", 0);
>>
>>        boolean cbValue = sp.getBoolean("CHECKBOX", false);
>>
>>        String name = sp.getString("NAME", "");
>>
>>        if(cbValue){
>>
>>        cb.setChecked(true);
>>
>>        }else{
>>
>>        cb.setChecked(false);
>>
>>        }
>>
>>        et.setText(name);
>>
>>  }
>>
>> }
>>
>>
>>
>>
>>
>> On Thu, Feb 21, 2013 at 4:46 PM, Mark Murphy <mmu...@commonsware.com>wrote:
>>
>>> Using the exact same code, if you wish.
>>>
>>> On Thu, Feb 21, 2013 at 7:40 PM, Marcelo Corvezola <mcorv...@gmail.com>
>>> wrote:
>>> > Normally I use something like the code below to load sharedPreference
>>> into
>>> > an activity..
>>> >
>>> > loadPrefs();
>>> >
>>> > }
>>> >
>>> >
>>> > private void loadPrefs() {
>>> >
>>> > SharedPreferences sp = PreferenceManager.**
>>> getDefaultSharedPreferences(**this);
>>> >
>>> > boolean cbValue = sp.getBoolean("CHECKBOX", false);
>>> >
>>> > String name = sp.getString("NAME", "YourName");
>>> >
>>> > if(cbValue){
>>> >
>>> > cb.setChecked(true);
>>> >
>>> > }else{
>>> >
>>> > cb.setChecked(false);
>>> >
>>> > }
>>> >
>>> > et.setText(name);
>>> >
>>> > }
>>> >
>>> >
>>> >
>>> > How are preferences loaded in a fragment Acivity? thnx
>>> >
>>> > --
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> > Groups "Android Developers" group.
>>> > To post to this group, send email to android-d...@**googlegroups.com
>>>
>>> > To unsubscribe from this group, send email to
>>> > android-developers+**unsubscr...@googlegroups.com
>>> > For more options, visit this group at
>>> > http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>
>>> > ---
>>> > You received this message because you are subscribed to the Google
>>> Groups
>>> > "Android Developers" group.
>>> > To unsubscribe from this group and stop receiving emails from it, send
>>> an
>>> > email to android-developers+**unsubscr...@googlegroups.com.
>>> > For more options, visit 
>>> > https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>> .
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Mark Murphy (a Commons Guy)
>>> http://commonsware.com | http://github.com/commonsguy
>>> http://commonsware.com/blog | http://twitter.com/commonsguy
>>>
>>> _The Busy Coder's Guide to Android Development_ Version 4.6 Available!
>>>
>>> --
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To post to this group, send email to android-d...@**googlegroups.com
>>>
>>> To unsubscribe from this group, send email to
>>> android-developers+**unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to android-developers+**unsubscr...@googlegroups.com.
>>> For more options, visit 
>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>> .
>>>
>>>
>>>
>>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-developers+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to