In my app I have common methods and attributes that I would like available to both List and regular activities. I want to use inheritance to create a parent class to extend to both types of activities. If I make the parent class extend list activity the regular activity will give me the exception:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘android.R.id.list’
Vica versa, I will not have the list functionality I need classes that extends this parent class.
How do I accomplish this without copying the same methods to two different classes that extend listactivity and activity?
Thanks!
No need to use ListActivity unless you’re looking for the simplest way to use a ListView.
The way I tackle this is chart out what activities I need, and what functionality I need across all of them, and only some, and
extendaccordingly.For example, you can have you super-base activity,
BaseActivity. This can have setup, teardown, and whatever other functionality you need for all your child activities. From there, you can have something likeBaseListActivitythat extendsBaseActivity, handles everything you may need fromBaseActivityand just add functionality for ListViews.Here’s a video of some classes I’ve been working on for managing Cursors as well as ListViews.
ManagedActivity classes – YouTube