ArrayAdapter already has a context it holds on to....

... so use PreferenceManager.getDefaultSharedPreferences(getContext()) or
getContext().getSharedPreferences(...)...

-- K

2012/10/16 lbendlin <l...@bendlin.us>

> Thank you for the insights.  However the adapter is not linked to any one
> activity - in fact I re-use it in multiple activities that have tables.
>  It's a class of its own. The lookup of the SharedPreferences is
> independent of the activity that may be using the adapter. So I'm afraid I
> cannot use the options you proposed.  (I am not using fragments)
>
>
> On Tuesday, October 16, 2012 5:09:10 AM UTC-4, Piren wrote:
>>
>> This doesn't really have anything to do with SharedPreferences or your
>> ListAdapter... this is Java basics.
>>
>> getApplication() isn't some global static function that can be called
>> from anywhere, it can be called however from an Activity. Since your
>> adapter is an instance with the Activity as a parent (most likely), you
>> need to reference it.
>>
>> If your activity is named MyActivity you can do:
>> SharedPreferences settings = PreferenceManager.getDefaultSh**
>> aredPreferences(MyActivity.**this.getApplication());
>>
>> (if its in a Fragment, MyActivity.this should be replaced with
>> getActivity() and so forth)
>>
>> On Tuesday, October 16, 2012 3:16:33 AM UTC+2, lbendlin wrote:
>>>
>>> I have a custom list view adapter
>>>
>>> *public* *class* CustomListAdapter *extends* ArrayAdapter<String>
>>>
>>>
>>> that I use to modify list view cells.  Now I have the need to modify
>>> some cells' content based on a global variable from the SharedPreferences.
>>>
>>> However, the adapter tells me it doesn't understand getApplication()  so
>>>
>>>
>>> SharedPreferences settings = PreferenceManager.**
>>> getDefaultSharedPreferences(**getApplication());
>>>
>>>
>>> doesn't work.  How else am I supposed to get the application context ?
>>> via a static variable in the application singleton?
>>>
>>  --
> 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 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

Reply via email to