Skip to content

Commit 5978bcc

Browse files
Jean-Baptiste QueruAndroid Code Review
authored andcommitted
Merge "Added an addAll to the ArrayAdapter"
2 parents a2e2d20 + 8c582ef commit 5978bcc

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

api/current.xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181308,6 +181308,32 @@
181308181308
<parameter name="object" type="T">
181309181309
</parameter>
181310181310
</method>
181311+
<method name="addAll"
181312+
return="void"
181313+
abstract="false"
181314+
native="false"
181315+
synchronized="false"
181316+
static="false"
181317+
final="false"
181318+
deprecated="not deprecated"
181319+
visibility="public"
181320+
>
181321+
<parameter name="collection" type="java.util.Collection&lt;? extends T&gt;">
181322+
</parameter>
181323+
</method>
181324+
<method name="addAll"
181325+
return="void"
181326+
abstract="false"
181327+
native="false"
181328+
synchronized="false"
181329+
static="false"
181330+
final="false"
181331+
deprecated="not deprecated"
181332+
visibility="public"
181333+
>
181334+
<parameter name="items" type="T...">
181335+
</parameter>
181336+
</method>
181311181337
<method name="clear"
181312181338
return="void"
181313181339
abstract="false"
@@ -197251,7 +197277,7 @@
197251197277
deprecated="not deprecated"
197252197278
visibility="public"
197253197279
>
197254-
<parameter name="arg0" type="T">
197280+
<parameter name="t" type="T">
197255197281
</parameter>
197256197282
</method>
197257197283
</interface>

core/java/android/widget/ArrayAdapter.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27+
import java.util.Collection;
2728
import java.util.List;
2829
import java.util.Comparator;
2930
import java.util.Collections;
@@ -83,7 +84,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable {
8384
*/
8485
private boolean mNotifyOnChange = true;
8586

86-
private Context mContext;
87+
private Context mContext;
8788

8889
private ArrayList<T> mOriginalValues;
8990
private ArrayFilter mFilter;
@@ -180,6 +181,44 @@ public void add(T object) {
180181
}
181182
}
182183

184+
/**
185+
* Adds the specified Collection at the end of the array.
186+
*
187+
* @param collection The Collection to add at the end of the array.
188+
*/
189+
public void addAll(Collection<? extends T> collection) {
190+
if (mOriginalValues != null) {
191+
synchronized (mLock) {
192+
mOriginalValues.addAll(collection);
193+
if (mNotifyOnChange) notifyDataSetChanged();
194+
}
195+
} else {
196+
mObjects.addAll(collection);
197+
if (mNotifyOnChange) notifyDataSetChanged();
198+
}
199+
}
200+
201+
/**
202+
* Adds the specified items at the end of the array.
203+
*
204+
* @param items The items to add at the end of the array.
205+
*/
206+
public void addAll(T ... items) {
207+
if (mOriginalValues != null) {
208+
synchronized (mLock) {
209+
for (T item : items) {
210+
mOriginalValues.add(item);
211+
}
212+
if (mNotifyOnChange) notifyDataSetChanged();
213+
}
214+
} else {
215+
for (T item : items) {
216+
mObjects.add(item);
217+
}
218+
if (mNotifyOnChange) notifyDataSetChanged();
219+
}
220+
}
221+
183222
/**
184223
* Inserts the specified object at the specified index in the array.
185224
*
@@ -236,7 +275,7 @@ public void clear() {
236275
*/
237276
public void sort(Comparator<? super T> comparator) {
238277
Collections.sort(mObjects, comparator);
239-
if (mNotifyOnChange) notifyDataSetChanged();
278+
if (mNotifyOnChange) notifyDataSetChanged();
240279
}
241280

242281
/**

0 commit comments

Comments
 (0)