Skip to content

Commit ffd0f98

Browse files
committed
Optimization for creating new bundle in bundle array when the existing bundle contains multiple keys
Optimize for the case: create new bundle in Bundle array, if there is an existing bundle which contains multiple keys, no need to re-create all the keys again in new created bundle, just automatically copy all keys from the existing one. This is extremely useful when the first bundle contains lots of keys.
1 parent e781e23 commit ffd0f98

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

app/src/main/java/com/afwsamples/testdpc/common/keyvaluepair/KeyValueBundleArrayFragment.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
3535
import java.util.List;
36+
import java.util.Set;
3637

3738
import static com.afwsamples.testdpc.common.EditDeleteArrayAdapter.OnDeleteButtonClickListener;
3839
import static com.afwsamples.testdpc.common.EditDeleteArrayAdapter.OnEditButtonClickListener;
@@ -129,7 +130,14 @@ protected void saveConfig() {
129130

130131
@Override
131132
protected void addNewRow() {
133+
134+
//Optimize for the case: create new bundle in Bundle array, if there is an existing bundle which contains multiple keys,
135+
//no need to re-create all the keys again in new created bundle, just automatically copy all keys from the existing one. This is extremely useful when the first bundle contains lots of keys.
132136
Bundle bundle = new Bundle();
137+
138+
if(mBundleList != null && mBundleList.size() > 0) {
139+
bundle = clearBundleValues((Bundle) mBundleList.get(0).clone());
140+
}
133141
mAdapter.add(bundle);
134142
showEditDialog(bundle);
135143
}
@@ -188,4 +196,22 @@ protected String getDisplayName(Bundle entry) {
188196
return String.valueOf("Bundle #" + mBundleList.indexOf(entry));
189197
}
190198
}
199+
200+
private Bundle clearBundleValues(Bundle bundle) {
201+
202+
Set<String> keySet = bundle.keySet();
203+
for(String key : keySet) {
204+
Object valueObject = bundle.get(key);
205+
if(valueObject instanceof String) {
206+
bundle.putString(key, "");
207+
} else if(valueObject instanceof Integer) {
208+
bundle.putInt(key, 0);
209+
} else if(valueObject instanceof Boolean) {
210+
bundle.putBoolean(key, false);
211+
} else if(valueObject instanceof Bundle) {
212+
bundle.putBundle(key, clearBundleValues((Bundle) valueObject));
213+
}
214+
}
215+
return bundle;
216+
}
191217
}

0 commit comments

Comments
 (0)