我有以下代码:
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
...
builder = newBuilder(id, Email.CONTENT_ITEM_TYPE, bUpdate);
if (bPref) {
builder.withValue(Email.TYPE, Email.TYPE_CUSTOM);
builder.withValue(Email.LABEL, "PREF");
} else if (iType != -1) {
builder.withValue(Email.TYPE, iType);
}
builder.withValue(Email.DATA, value);
ops.add(builder.build());
builder = newBuilder(id, Phone.CONTENT_ITEM_TYPE, bUpdate);
if (bPref) {
builder.withValue(Phone.TYPE, Phone.TYPE_CUSTOM);
builder.withValue(Phone.LABEL, "PREF");
} else if (iType != -1) {
builder.withValue(Phone.TYPE, iType);
}
builder.withValue(Phone.DATA, value);
ops.add(builder.build());
builder = ...
...
这些块非常相似,我想为它们编写一个函数。
例如:
void build(xxx name) {
...
if (bPref) {
builder.withValue(name.TYPE, name.TYPE_CUSTOM);
builder.withValue(name.LABEL, "PREF");
} else if (iType != -1) {
builder.withValue(name.TYPE, iType);
}
builder.withValue(name.DATA, value);
ops.add(builder.build());
}
...
build(Email);
build(Phone);
build(...);
或者有没有办法将这些重构为函数?