Re: Initializing from context passing
On 23/04/2011 17:35, Daniele Futtorovic wrote:
On 23/04/2011 17:39, Dirk Bruere at NeoPax allegedly wrote:
I have a Data class of the form:
Data
(
public static ListView albumsLV;
public static ArrayAdapter<String> albumsTitleAdapter
public static ArrayList<String> albumsTitleArrayList = new
ArrayList<String>();
Data(Context ctx)
{
albumsLV = (ListView )((Activity) ctx).findViewById(R.id.ListViewAlbums);
albumsTitleAdapter = new ArrayAdapter<String>(ctx,
android.R.layout.simple_expandable_list_item_1,albumsTitleArrayList);
}
}
and in the main method{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context currentContext = this;
Data data = new Data(currentContext);
}
lvAlbum = (ListView)findViewById(R.id.ListViewAlbums);
lvAlbum.setAdapter(Data.albumsTitleAdapter);
}
I never use Data data variable again.
Is that sufficient to initialize the static parameters in Data class?
Yeah, but it's crap. At least make it a static method e.g.
Data.init(Context).
And do use interface types rather than implementations, unless you
specifically rely on a detail of the implementation (which is often a
bad sign). That is, make it a List<T>, not an ArrayList<T>.
I would appreciate a pointer to an example.
Everything I have come across in the Android examples I have seen uses
ArrayAdapter
--
Dirk
http://www.neopax.com/technomage/ - My new book - Magick and Technology