In my android application I have some fields like
MyButton a;
MyEditText b;
MySpinner c;
MyTextView d;
Each of these is like public class MyButton extends Button implements InfoExtract and
public interface InfoExtract{
String getTheText();
}
Out of these fields (the user can change some of them), there is a UserProfile to be created like so:
public class ProfileUpdate {
// The fields have to be string no matter what
String firstName;
String lastName;
String dateOfBirth;
String relationshipStatus
}
The execution flow is like that:
List<InfoExtract> uiElements = new ArrayList<InfoExtract>();
uiElements.add(a);
uiElements.add(b);
uiElements.add(c);
uiElements.add(d);
someButton.setOnClickListener(new SaveProfileListener(uiElements);
The SaveProfileListener more or less does this:
ProfileUpdate pup = new ProfileUpdate();
int i = 0;
pup.firstName = uiElements.get(i++).getTheText()
pup.lastName = uiElements.get(i++).getTheText();
pup.dateOfBirth = uiElements.get(i++).getTheText();
pup.relationshipStatus = uiElements.get(i++).getTheText();
Bad thing #1: If I want to add another fields its obviously much work.
Bad thing #2: The order of elements in the list is important.
Bad thing #3: I cannot easily manipulate the date format for dateOfBirth for example.
What i could have done but what is equally bad:
// Pass the listener what fields it should use.
someButton.setOnClickListener(new SaveProfileListener(a, b, c, d);
How to make this nice and clean?
I ended up using a Map and a new Enum type for each field.