Kostya,

Bingo!  I didn't realize that you can use your own subclasses in the XML
file!  And I also tripped over that "convert between integers and strings"
issue way back when.  Thank goodness for being able to Google and fine
that it was a known problem with a known workaround.

Now I have to decide if it's worth subclassing PreferenceCategory or if
I should just use instanceof.  Since it's just PreferenceCategory that
would change in my XML its probably wortwhile doing - and potentially more
stable in the long-term than instanceof.

Now to Google and figure out how to use my own subclass in an XML
preferences file!

Thanks!

...Jake


>>>>> "KV" == Kostya Vasilyev <[email protected]> writes:

   KV> Jake,

   KV> My turn to think I'm missing something.

   KV> I imagine you have a subclass of PreferenceActivity, with
   KV> onCreate calling addPreferencesFromResource(R.xml.<some xml id
   KV> here>).

   KV> In that XML file, you have your preference item definitions.

   KV> You can use your own {sub}classes in that XML file, just like
   KV> with layouts and views (e.g. for a list-of-options type
   KV> preference, I got sick of having to convert between integers and
   KV> strings at some point, which is necessary if you use
   KV> ListPreference, and wrote my own class that uses a list of
   KV> integers).

   KV> Now as it turns out, PreferenceScreen is "final" (what the?) so
   KV> you can't subclass that, but PreferenceCategory is not final, so
   KV> it can be subclassed and specified in the XML.

   KV> -- Kostya

   KV> 2011/5/26 Jake Colman <[email protected]>

   >> 
   >> Kostya,
   >> 
   >> Maybe I'm missing something obvious here but where do I get an
   >> opportunity to provide Android with my extended PreferenceGroup or
   >> PreferenceScreen?  I need to make sure that my addPreference method
   >> called during the processing sequence initiated by
   >> addPreferencesFromResource.  But how would that code know to use my
   >> extended PreferenceScreen so that it uses my overridden addPreference
   >> method?  Is there a 'set' method somewhere that allows me to specify my
   >> extended class?
   >> 
   >> ...Jake
   >> 
   >> 
   >> >>>>> "KV" == Kostya Vasilyev <[email protected]> writes:
   >> 
   KV> Preference.onSetInitialValue is triggered by
   KV> addPreferencesFromResource, which in turn is called from within
   KV> your activity's onCreate - not before.
   >> 
   KV> However, this happens before your code has a change to call
   KV> findPreference / setKey.
   >> 
   KV> One of the methods that gets called while this is taking place is
   KV> PreferenceGroup.addPreference. This could be a reasonable place
   KV> to reset the keys, so it happens before onSetInitialValue. The
   KV> actual subclasses are PreferenceScreen and PreferenceCategory.
   >> 
   KV> You can check for a specifc runtime class by using Java
   KV> 'instanceof' operator (but this should not be necessary with
   KV> overiding addPreference).
   >> 
   KV> -- Kostya
   >> 
   KV> 2011/5/26 Jake Colman <[email protected]>
   >> 
   >> >> >>>>> "S" == String  <[email protected]> writes:
   >> >>
   S> On Thursday, May 26, 2011 2:44:37 AM UTC+1, Jake Colman wrote:
   >> >>
   S> The only issue is when I launch the Preference
   >> >> >> Activity from the widget it does not display the widget-specific
   >> >> value
   >> >> >> but displays the preference's default value.
   >> >> >>
   S> Are you actively setting the value for each of your
   S> widget-specific preferences, or are you just setting the key and
   S> hoping they'll pick up the correct (initial) value from the
   S> SharedPreferences? I think you need to do the former rather than
   S> the latter.
   >> >>
   S> IOW, my experience is that the initialization of the preferences
   S> in a PreferenceActivity happens before your onCreate() handler is
   S> called, so changing preferences' keys there won't affect their
   S> displayed values. You should just need a simple call to
   S> setChecked() for each boolean preference to initialize it to the
   S> appropriate, widget-specific value.
   >> >>
   >> >> String,
   >> >>
   >> >> What I am seeing is consistent with what you stated, namely, that by
   >> the
   >> >> time onCreate is called, the preferences are already initialized so
   >> the
   >> >> displayed values have already been set.  But why does this only effect
   >> >> my CheckBoxPreferences whil my ListPrefeference values are being set
   >> >> correctly?
   >> >>
   >> >> When I call findPreference is there a way for me to determine the
   >> >> specific derived Preference class that I acually got?  With C++ I can
   >> >> dynamic_cast the base class to find out its actual type.  How does one
   >> >> do the same in Java?  If so, then in the method that resets the
   >> generic
   >> >> preference key to a widget-specific key I can check whether the
   >> >> Preference is actually a checkbox and then get and set its actual
   >> value.
   >> >>
   >> >> Hmmmmm.  Maybe I should just write some code to always just drill into
   >> >> the SharedPreferences and manually set the value across all types.  As
   >> >> long as I can query the Preference object to get its specific type, I
   >> >> can generically explicitly set the value for all preferences.
   >> >>
   >> >> --
   >> >> Jake Colman -- Android Tinkerer
   >> >>
   >> >> --
   >> >> You received this message because you are subscribed to the Google
   >> >> Groups "Android Developers" group.
   >> >> To post to this group, send email to
   >> [email protected]
   >> >> To unsubscribe from this group, send email to
   >> >> [email protected]
   >> >> For more options, visit this group at
   >> >> http://groups.google.com/group/android-developers?hl=en
   >> >>
   >> 
   KV> --
   KV> You received this message because you are subscribed to the Google
   KV> Groups "Android Developers" group.
   KV> To post to this group, send email to
   >> [email protected]
   KV> To unsubscribe from this group, send email to
   KV> [email protected]
   KV> For more options, visit this group at
   KV> http://groups.google.com/group/android-developers?hl=en
   >> 
   >> --
   >> Jake Colman -- Android Tinkerer
   >> 
   >> --
   >> You received this message because you are subscribed to the Google
   >> Groups "Android Developers" group.
   >> To post to this group, send email to [email protected]
   >> To unsubscribe from this group, send email to
   >> [email protected]
   >> For more options, visit this group at
   >> http://groups.google.com/group/android-developers?hl=en
   >> 

   KV> -- 
   KV> You received this message because you are subscribed to the Google
   KV> Groups "Android Developers" group.
   KV> To post to this group, send email to [email protected]
   KV> To unsubscribe from this group, send email to
   KV> [email protected]
   KV> For more options, visit this group at
   KV> http://groups.google.com/group/android-developers?hl=en

-- 
Jake Colman -- Android Tinkerer

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to