splitting one java file into multiple class files

644 views
Skip to first unread message

Michael Murphy

unread,
Mar 19, 2010, 8:46:56 PM3/19/10
to cw-android
I'm working through Mark's "Android Programming Tutorials" and was
wondering about the practice of breaking one big java class file into
multiple files.

Specifically, I took the LunchList.java file and pulled the
RestaurantWrapper class out into a seperate java file and everything
is working fine (I'm up through the point of adding the tabs to the
project). When I tried to pull the RestaurantAdapter class out,
things broke. I was getting errors in reference to the model parameter
within the super method and the LayoutInflater wasn't happy either.

Is it good practice to separate classes into their own files? What do
I need to change to make the RestaurantAdapter class work as a
standalone java file?

Thanks,
Mike

Mark Murphy

unread,
Mar 19, 2010, 8:59:42 PM3/19/10
to cw-an...@googlegroups.com
Michael Murphy wrote:
> I'm working through Mark's "Android Programming Tutorials" and was
> wondering about the practice of breaking one big java class file into
> multiple files.
>
> Specifically, I took the LunchList.java file and pulled the
> RestaurantWrapper class out into a seperate java file and everything
> is working fine (I'm up through the point of adding the tabs to the
> project). When I tried to pull the RestaurantAdapter class out,
> things broke. I was getting errors in reference to the model parameter
> within the super method and the LayoutInflater wasn't happy either.
>
> Is it good practice to separate classes into their own files?

That's a bit difficult to answer in the abstract.

> What do
> I need to change to make the RestaurantAdapter class work as a
> standalone java file?

RestaurantAdapter takes advantage of being an inner class to directly
call methods on the outer class (LunchList), such as
getLayoutInflater(). If you wish to make RestaurantAdapter its own
top-level class in its own file, you will need to convert all those calls.

Since the adapter base classes support getContext(), which should return
the Activity, you can convert things like:

getLayoutInflater(...)

to:

((Activity)getContext()).getLayoutInflater(...)

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Warescription: Three Android Books, Plus Updates, One Low Price!

Reply all
Reply to author
Forward
0 new messages