Kostya/String,

Is nothing ever easy?  So close, yet so far...  :-)

Some of my preferences have dependent keys.  Since the keys are being
renamed to be widget-specific I, of course, have to rename the dependent
keys accordingly as well.

Pref2 is dependent on Pref1.  Pref1 comes through my overridden
onPrepareAddPreference and I call setKey to rename its key.  Pref2 comes
through onPrepareAddPreference and I reset its key as usual and I also
reset its dependent key to have the correct Pref1 key name.  This
triggers a java.lang.IllegalStateException stating that Dependency not
found for preference. 

Since Pref1 has already been reset I would expect that by the time Pref2
comes in Pref1 is already out there.  Also, I did not think that
ordering would matter anyway as long as all the keys make sense at the
of the inflation.

Any ideas?

Incidentally, I tried both onPrepareAddPreference and addPreference just
in case one worked differently than the other.  And, yes, I called up to
super at the end of my overridden method.

...Jake


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

   KV> Yes, a cast is what I'm suggesting. From the design point of
   KV> view, you'll end up with two classes that are meant to be used
   KV> together.

   KV> You could even do an instanceof check before the cast, and throw
   KV> an exception with a nice descriptive message if the check returns
   KV> false :)

   KV> --
   KV> Kostya Vasilyev
   KV> 27.05.2011 1:56 пользователь "Jake Colman" <[email protected]>
   KV>            написал:
   >> 
   >> 
   >> All the magic happens from addPreferencesFromResource which inflates my
   >> preferences XML and instantiates the Preference objects. With
   >> everything we've discussed until now, I know what/how to subclass so
   >> that I can hook into things at the correct level to reset my key. But
   >> if it's WidgetOptionsActivity that has the widget id (from its super
   >> class) how does that get passed into addPreferencesFromResource?
   >> 
   >> Unless you are suggesting that I can call a method via the context which
   >> is, in essence, my Activity? So cast the context into the Activity and
   >> call the method?
   >> 
   >> 
   >>>>>>> "KV" == Kostya Vasilyev <[email protected]> writes:
   >> 
   KV> Preferences are inflated with a Context, which in this case is
   KV> your PreferenceActivity subclass.
   >> 
   KV> I'd make two classes, WidgetOptionsActivity and
   KV> WidgetOptionsPreferenceCategory, with the expectation that the
   KV> latter is always used within the former (like maps API and
   KV> MapActivity).
   >> 
   KV> The widget id is passed to the activity as an intent extra, so
   KV> just get it from there and make available as a method / field
   KV> before you call addPreferencesFromResource().
   >> 
   KV> Then you could derive YourProjectNameWidgetOptionsActivity from
   KV> WidgetOptionsActivity.
   >> 
   KV> -- Kostya
   >> 
   KV> 2011/5/27 Jake Colman <[email protected]>
   >> 
   >> >>
   >> >> Thanks Kostya and String, these were great answers.
   >> >>
   >> >> I can see how I can create my own subclass and use them instead of my
   >> >> the android classes. My problem now is that it is the Preference
   >> >> Activity, through an intent extra, that knows the specific widget ID
   KV> for
   >> >> which it is being displayed. I don't see that the Preference or
   >> >> PreferenceCategory subclassses have access to the parent activity that
   >> >> caused them to be created. Since Android is instantiating my subclasses
   >> >> for me, I don't see how I have a hook to pass the subclass the crucial
   >> >> piece of data required to reset its key - which is the whole purpose of
   >> >> this exercise.
   >> >>
   >> >> It looks like onPrepareAddPreference is probable the better place to
   >> >> reset the key than is addPreference simply because this is more of a
   >> >> preparation step anyway. It's great to have multiple options!
   >> >>
   >> >> ...Jake
   >> >>
   >> >>
   >> >> >>>>> "KV" == Kostya Vasilyev <[email protected]> writes:
   >> >>
   KV> Where your preference class needs to have the standard
   KV> constructor, just like with views:
   >> >>
   KV> public class MyPreferenceCategory extends PreferenceCategory {
   >> >>
   KV> public MyPreferenceCategory(Context context, AttributeSet attrs)
   >> >> {
   KV> super(context, attrs);
   KV> }
   >> >>
   KV> }
   >> >>
   KV> And here is another useful override:
   >> >>
   KV> 
   >> >>
   KV> 
http://developer.android.com/reference/android/preference/PreferenceGroup.html#onPrepareAddPreference(android.preference.Preference
   KV> )
   >> >>
   KV> onPrepareAddPreference
   >> >>
   KV> -- Kostya
   >> >>
   KV> 2011/5/27 String <[email protected]>
   >> >>
   >> >> >> In your XML settings-definition file, here's what you need. Just
   KV> use:
   >> >> >>
   >> >> >> <com.my.package.PrefSubclass
   >> >> >> android:key="pref_key"
   >> >> >> ...
   >> >> >> />
   >> >> >>
   >> >> >> where "com.my.package" is your package name, and PrefSubclass is the
   >> >> class
   >> >> >> name of your Preference subclass. Use this instead of:
   >> >> >>
   >> >> >> <EditTextPreference
   >> >> >> android:key="pref_key"
   >> >> >> ...
   >> >> >> />
   >> >> >>
   >> >> >> or whatever Preference class you're basing it off of.
   >> >> >>
   >> >> >> String
   >> >> >>
   >> >> >> --
   >> >> >> 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
   KV> [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
   KV> [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