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

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

--
Kostya Vasilyev
27.05.2011 1:56 пользователь "Jake Colman" <[email protected]> написал:
>
>
> 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
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>
> >>
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
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
[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

-- 
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