I've got a ListActivity
with about 100 events. (These events are also displayed on a map in another activity.)
So I've my MyListActivity
, which handles the list and a MyListAdapter
to populate the list that deals with MyEvent
-Objects. As model I have a MyEvent
-model-Class and a MyEventStorage
-Class.
Now have written a method to return an image for an event based on its ID. It does some decisions which image to load, where it gets the image from, loads it and resamples it.
Where should I put this method in best practice?
I don't want to copy it in every activity where it is needed but just in one place.
I'd like to have it in my
MyEvent
-Class, so I can callmyEvent.getImage();
but it somehow feels wrong to put this method inside the model class with all the getters and setters. Is it wrong?Should I write a helper class containing this method? As a static method? Would this still provide a good performance?
Or maybe create an additional
MyImageGetter
-object for every MyEvent-object?Or expand the
MyEvent
-model with an image-variable and getters/setter and create an extra class that puts the proper image in the model? How would I call that method?Another solution?
MyEvent.java:
public class MyEvent {
private int id;
private int category;
private String eventname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// other getters and setters
}
MyEventStorage.java:
private static MyEventStorage instance = null;
private List<MyEvent> store;
private MyEventStorage() {
store = new ArrayList<MyEvent>();
}
// get the storage containing the events
public static MyEventStorage getInstance() {
if (instance == null) {
instance = new MyEventStorage();
}
return instance;
}
public List<MyEvent> getStore() {
return store;
}
public void setStore(List<MyEvent> store) {
this.store = store;
}
// Add a Event to the store
public void addEvent(MyEvent myEvent) {
store.add(myEvent);
}
// Remove a Event from the store
public void removeEvent(MyEvent myEvent) {
store.remove(myEvent);
}
}
The method I want to integrate:
Image getImageById(int id) {
// decide which image to load based on the events id
// decide where to load the image from
// check if image available
// load image if available else load placeholder image
// resample image
return image;
}
Thank you advance!