Skip to content

Commit e4686d1

Browse files
committed
fixed counting bug in CompoundAdapter, please test your code if you have relied on the previous error
1 parent 9f66a01 commit e4686d1

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

src/main/java/com/edmondapps/utils/android/ui/CompoundAdapter.java

+35-58
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
*/
1616
package com.edmondapps.utils.android.ui;
1717

18-
import java.util.ArrayList;
19-
import java.util.Iterator;
20-
import java.util.List;
21-
2218
import android.database.DataSetObserver;
2319
import android.view.View;
2420
import android.view.ViewGroup;
2521
import android.widget.BaseAdapter;
2622
import android.widget.ListAdapter;
2723

24+
import java.util.ArrayList;
25+
import java.util.Iterator;
26+
import java.util.List;
27+
2828
/**
2929
* An adapter that combines multiple adapters.
3030
* </p>
@@ -37,12 +37,11 @@
3737
* that is responsible for the position, and use
3838
* {@link #getPositionForAdapter(int)} to get the position for the adapter
3939
* (instead of the position in the {@code CoumpoundAdapter}).
40-
* <p>
40+
* <p/>
4141
* You may also use {@link #getAdapterInfo(int)} and
4242
* {@link #getPositionForAdapter(int, AdapterInfo)} if the situation requires.
43-
*
43+
*
4444
* @author Edmond
45-
*
4645
*/
4746
public class CompoundAdapter extends BaseAdapter {
4847

@@ -52,16 +51,15 @@ public class CompoundAdapter extends BaseAdapter {
5251
* Most of the time you don't have to interact with it, however, a common
5352
* use case is shown below, <code>
5453
* <pre>
55-
public Object getItem(int position) {
56-
AdapterInfo info = getAdapterInfo(position);
57-
int positionForAdapter = getPositionForAdapter(position, info);
58-
return info.getAdapter().getItem(positionForAdapter);
59-
}
54+
* public Object getItem(int position) {
55+
* AdapterInfo info = getAdapterInfo(position);
56+
* int positionForAdapter = getPositionForAdapter(position, info);
57+
* return info.getAdapter().getItem(positionForAdapter);
58+
* }
6059
* </pre>
6160
* </code>
62-
*
61+
*
6362
* @author Edmond
64-
*
6563
*/
6664
public static final class AdapterInfo {
6765
private final BaseAdapter mAdapter;
@@ -77,7 +75,6 @@ private AdapterInfo(BaseAdapter adapter) {
7775
}
7876

7977
/**
80-
*
8178
* @return the adapter that this {@code AdapterInfo} is about
8279
*/
8380
public BaseAdapter getAdapter() {
@@ -97,19 +94,17 @@ private int getViewTypeCount() {
9794
}
9895

9996
/**
100-
*
10197
* @return the position of this adapter in the {@code CompoundAdapter}
10298
*/
10399
public final int getPosition() {
104100
return mPosition;
105101
}
106102

107103
/**
108-
*
109-
* @see ListAdapter#getItemViewType(int)
110104
* @return the internal view type offset that is used to distinguish
111-
* view types
112-
* of different adapters
105+
* view types
106+
* of different adapters
107+
* @see ListAdapter#getItemViewType(int)
113108
*/
114109
public final int getViewTypeOffset() {
115110
return mViewTypeOffset;
@@ -129,9 +124,8 @@ private void setViewTypeOffset(int viewTypeOffset) {
129124
/**
130125
* Constructs a {@code CompoundAdapter} that contains a single adapter. You
131126
* may use {@link #addAdapter(BaseAdapter)} later on.
132-
*
133-
* @param adapter
134-
* a non-null instance of a {@code BaseAdapter}
127+
*
128+
* @param adapter a non-null instance of a {@code BaseAdapter}
135129
*/
136130
public CompoundAdapter(BaseAdapter adapter) {
137131
AdapterInfo info = new AdapterInfo(adapter);
@@ -143,11 +137,9 @@ public CompoundAdapter(BaseAdapter adapter) {
143137
/**
144138
* Constructs a {@code CompoundAdapter} that contains a 2 adapters. The
145139
* parameters order determines the order of the list from top to bottom.
146-
*
147-
* @param adapter
148-
* the first non-null instance of a {@code BaseAdapter}
149-
* @param adapter2
150-
* the second non-null instance of a {@code BaseAdapter}
140+
*
141+
* @param adapter the first non-null instance of a {@code BaseAdapter}
142+
* @param adapter2 the second non-null instance of a {@code BaseAdapter}
151143
*/
152144
public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2) {
153145
AdapterInfo info = new AdapterInfo(adapter);
@@ -161,13 +153,10 @@ public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2) {
161153
/**
162154
* Constructs a {@code CompoundAdapter} that contains more than 2 adapters.
163155
* The parameters order determines the order of the list from top to bottom.
164-
*
165-
* @param adapter
166-
* the first non-null instance of a {@code BaseAdapter}
167-
* @param adapter2
168-
* the second non-null instance of a {@code BaseAdapter}
169-
* @param adapters
170-
* the other instances of {@code BaseAdapter}
156+
*
157+
* @param adapter the first non-null instance of a {@code BaseAdapter}
158+
* @param adapter2 the second non-null instance of a {@code BaseAdapter}
159+
* @param adapters the other instances of {@code BaseAdapter}
171160
*/
172161
public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2, BaseAdapter... adapters) {
173162
AdapterInfo info = new AdapterInfo(adapter);
@@ -184,11 +173,8 @@ public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2, BaseAdapter...
184173
}
185174

186175
/**
187-
*
188-
* @param position
189-
* the position of the {@code CompoundAdapter}
190-
* @param info
191-
* usually retrieved by {@link #getAdapterInfo(int)}
176+
* @param position the position of the {@code CompoundAdapter}
177+
* @param info usually retrieved by {@link #getAdapterInfo(int)}
192178
* @return the position of the adapter contained in the {@code AdapterInfo}
193179
*/
194180
public final static int getPositionForAdapter(int position, AdapterInfo info) {
@@ -204,11 +190,8 @@ public final int getPositionForAdapter(int position) {
204190
}
205191

206192
/**
207-
*
208-
* @param position
209-
* the position of the {@code CompoundAdapter}
210-
* @param info
211-
* usually retrieved by {@link #getAdapterInfo(int)}
193+
* @param position the position of the {@code CompoundAdapter}
194+
* @param info usually retrieved by {@link #getAdapterInfo(int)}
212195
* @return the view type of the adapter contained in the {@code AdapterInfo}
213196
*/
214197
public final static int getViewTypeForAdapter(int position, AdapterInfo info) {
@@ -227,10 +210,9 @@ public final int getViewTypeForAdapter(int position) {
227210
/**
228211
* Append an adapter to the end of this {@code CompoundAdapter}.<br>
229212
* This {@code CompoundAdapter} will also refresh itself.
230-
*
213+
*
214+
* @param adapter a non-null instance of a {@code BaseAdapter}
231215
* @see #notifyDataSetChanged()
232-
* @param adapter
233-
* a non-null instance of a {@code BaseAdapter}
234216
*/
235217
public void addAdapter(BaseAdapter adapter) {
236218
mAdapters.add(new AdapterInfo(adapter));
@@ -241,11 +223,10 @@ public void addAdapter(BaseAdapter adapter) {
241223
* Performs a linear search of the adapters in this {@code CompoundAdapter}
242224
* to remove the provided adapter. If it does remove the adapter, this
243225
* adapter will refresh itself.
244-
*
245-
* @see #notifyDataSetChanged()
246-
* @param adapter
247-
* an instance of {@code BaseAdapter}
226+
*
227+
* @param adapter an instance of {@code BaseAdapter}
248228
* @return if it has removed the provided adapter
229+
* @see #notifyDataSetChanged()
249230
*/
250231
public boolean removeAdapter(BaseAdapter adapter) {
251232
Iterator<AdapterInfo> it = mAdapters.iterator();
@@ -269,17 +250,13 @@ private void updateOffset() {
269250
info.setPosition(count);
270251
info.setViewTypeOffset(viewTypeCount);
271252

272-
if (!info.isEmpty()) {
273-
count += info.getCount();
274-
}
253+
count += info.getCount();
275254
viewTypeCount += info.getViewTypeCount();
276255
}
277256
}
278257

279258
/**
280-
*
281-
* @param position
282-
* position of the {@code CompoundAdapter}
259+
* @param position position of the {@code CompoundAdapter}
283260
* @return the info about the adapter that is managing this position
284261
*/
285262
public final AdapterInfo getAdapterInfo(int position) {

0 commit comments

Comments
 (0)