Skip to content

Commit 37ce7f8

Browse files
authored
Merge pull request #115 from johnlee2898/bundle-array-create-new-bundle
Optimization for creating new bundle in bundle array when the existin…
2 parents e781e23 + ffd0f98 commit 37ce7f8

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)