1
在我的Android應用程序,我有一個像如何以良好的方式從UI元素創建對象?
MyButton a;
MyEditText b;
MySpinner c;
MyTextView d;
某些領域每一種是像public class MyButton extends Button implements InfoExtract
和
public interface InfoExtract{
String getTheText();
}
在這些領域(用戶可以更改其中的一些),有一個用戶配置要像這樣創建的:
public class ProfileUpdate {
// The fields have to be string no matter what
String firstName;
String lastName;
String dateOfBirth;
String relationshipStatus
}
的執行流程是這樣的:
List<InfoExtract> uiElements = new ArrayList<InfoExtract>();
uiElements.add(a);
uiElements.add(b);
uiElements.add(c);
uiElements.add(d);
someButton.setOnClickListener(new SaveProfileListener(uiElements);
的SaveProfileListener
或多或少地做到這一點:
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();
壞事#1:如果我想添加其他領域的顯着大量的工作。
壞事#2:列表中元素的順序很重要。
不好的事情#3:例如,我不能輕易操作dateOfBirth的日期格式。
什麼我可以做,但什麼是同樣糟糕:
// Pass the listener what fields it should use.
someButton.setOnClickListener(new SaveProfileListener(a, b, c, d);
如何使這個漂亮和乾淨的?