|
33 | 33 | import java.util.ArrayList;
|
34 | 34 | import java.util.Arrays;
|
35 | 35 | import java.util.List;
|
| 36 | +import java.util.Set; |
36 | 37 |
|
37 | 38 | import static com.afwsamples.testdpc.common.EditDeleteArrayAdapter.OnDeleteButtonClickListener;
|
38 | 39 | import static com.afwsamples.testdpc.common.EditDeleteArrayAdapter.OnEditButtonClickListener;
|
@@ -129,7 +130,14 @@ protected void saveConfig() {
|
129 | 130 |
|
130 | 131 | @Override
|
131 | 132 | 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. |
132 | 136 | Bundle bundle = new Bundle();
|
| 137 | + |
| 138 | + if(mBundleList != null && mBundleList.size() > 0) { |
| 139 | + bundle = clearBundleValues((Bundle) mBundleList.get(0).clone()); |
| 140 | + } |
133 | 141 | mAdapter.add(bundle);
|
134 | 142 | showEditDialog(bundle);
|
135 | 143 | }
|
@@ -188,4 +196,22 @@ protected String getDisplayName(Bundle entry) {
|
188 | 196 | return String.valueOf("Bundle #" + mBundleList.indexOf(entry));
|
189 | 197 | }
|
190 | 198 | }
|
| 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 | + } |
191 | 217 | }
|
0 commit comments