Initializing from context passing

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

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

Daniele Futtorovic

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

Dirk Bruere at NeoPax

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
 
D

Daniele Futtorovic

So do not do it in the constructor?

/Where/ you do it isn't as much the point as whether you do it
statically or not.
If your data is of static nature (i.e. bound to the program, global,
rather than bound to a context), then it is wrong to initialise it
through an instance.

Of course, you might also do this...

##############################################
private static volatile Data singleton;

public static void init( Context context )
throws InitialisationException
{
singleton = new Data( context );
}

public static Data getSharedInstance(){
if( singleton == null ) throw new IllegalStateException("not
initialised");
return singleton;
}

private final ListView albumsLV;
private final ArrayAdapter<String> albumsTitleAdapter
private final List<String> albumsTitleArrayList = new ArrayList<String>();

Data(Context ctx)
throws InitialisationException
{
albumsLV = (ListView )((Activity)
ctx).findViewById(R.id.ListViewAlbums);
albumsTitleAdapter = new ArrayAdapter<String>(ctx,
android.R.layout.simple_expandable_list_item_1,albumsTitleArrayList);
}

public ListView getAlbumsLV(){ return albumsLV; }
public ArrayAdapter<String> getAlbumsTitleAdapter(){ return
albumsTitleAdapter; }
public List<String> getAlbumsTitleList(){ return albumsTitleArrayList; }
}
##############################################

.... and your initialisation would happen in a constructor, be bound to
an instance and that would be fine. But still you'd access it
statically, therefore it's static data. And static data ought to be
initialised statically.
 
L

Lew

I would appreciate a pointer to an example.
Everything I have come across in the Android examples I have seen uses
ArrayAdapter

They said to use 'List' instead of 'ArrayList', not instead of 'ArrayAdapter'!

It makes absolutely no sense, and would not compile, to use 'List' in lieu of
'ArrayAdapter', n'est-ce pas?
 
D

Dirk Bruere at NeoPax

They said to use 'List' instead of 'ArrayList', not instead of
'ArrayAdapter'!

It makes absolutely no sense, and would not compile, to use 'List' in
lieu of 'ArrayAdapter', n'est-ce pas?

True, my mistake.
But having said that, all the examples I have seen use ArrayList and not
List
 
L

Lew

Dirk said:
But having said that, all the examples I have seen use ArrayList and not List

I will assume you mean for the declared type of the variable and not the
runtime instantiated type, since the latter cannot be 'List' as such.

The question is why they do. Several possibilities exist, including but not
limited to:

- The API with which they interact is so poorly designed as to specify
'ArrayList' arguments or return types.

- You are going to really, really poor sources for examples.

- The pedagogical purpose of the examples has nothing to do whatsoever with
the best practice of declaring the wider type, therefore they disregarded it.

Regardless, only the first reason is compelling, and in the case of
'android.widget.ArrayAdapter<T>', inapplicable.

Just because the examples you saw used 'ArrayList' as declared variable types,
the fact that the API library only requires 'List' should have tipped you off.
Programming is an art that requires thinking. The other red flag is that
the declaration of 'ArrayList' variables is a violation of a best practice,
and should send one scurrying about looking for why it was done, not
unquestioningly following cattle to the slaughterhouse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top