28
28
import java .util .List ;
29
29
import java .util .Map ;
30
30
31
- public class FirebaseIndexArray <T > extends CachingObservableSnapshotArray <T > implements ChangeEventListener {
31
+ public class FirebaseIndexArray <T > extends CachingObservableSnapshotArray <T >
32
+ implements ChangeEventListener {
32
33
private static final String TAG = "FirebaseIndexArray" ;
33
34
34
35
private DatabaseReference mDataRef ;
@@ -107,7 +108,7 @@ protected void onDestroy() {
107
108
public void onChildChanged (EventType type , DataSnapshot snapshot , int index , int oldIndex ) {
108
109
switch (type ) {
109
110
case ADDED :
110
- onKeyAdded (snapshot );
111
+ onKeyAdded (snapshot , index );
111
112
break ;
112
113
case MOVED :
113
114
onKeyMoved (snapshot , index , oldIndex );
@@ -175,16 +176,16 @@ private boolean isKeyAtIndex(String key, int index) {
175
176
return index >= 0 && index < size () && mDataSnapshots .get (index ).getKey ().equals (key );
176
177
}
177
178
178
- protected void onKeyAdded (DataSnapshot data ) {
179
+ private void onKeyAdded (DataSnapshot data , int newIndex ) {
179
180
String key = data .getKey ();
180
181
DatabaseReference ref = mDataRef .child (key );
181
182
182
183
mKeysWithPendingUpdate .add (key );
183
184
// Start listening
184
- mRefs .put (ref , ref .addValueEventListener (new DataRefListener ()));
185
+ mRefs .put (ref , ref .addValueEventListener (new DataRefListener (newIndex )));
185
186
}
186
187
187
- protected void onKeyMoved (DataSnapshot data , int index , int oldIndex ) {
188
+ private void onKeyMoved (DataSnapshot data , int index , int oldIndex ) {
188
189
String key = data .getKey ();
189
190
190
191
// We can't use `returnOrFindIndexForKey(...)` for `oldIndex` or it might find the updated
@@ -199,7 +200,7 @@ protected void onKeyMoved(DataSnapshot data, int index, int oldIndex) {
199
200
}
200
201
}
201
202
202
- protected void onKeyRemoved (DataSnapshot data , int index ) {
203
+ private void onKeyRemoved (DataSnapshot data , int index ) {
203
204
String key = data .getKey ();
204
205
ValueEventListener listener = mRefs .remove (mDataRef .getRef ().child (key ));
205
206
if (listener != null ) mDataRef .child (key ).removeEventListener (listener );
@@ -243,10 +244,14 @@ public String toString() {
243
244
/**
244
245
* A ValueEventListener attached to the joined child data.
245
246
*/
246
- protected class DataRefListener implements ValueEventListener {
247
+ private final class DataRefListener implements ValueEventListener {
247
248
/** Cached index to skip searching for the current index on each update */
248
249
private int currentIndex ;
249
250
251
+ public DataRefListener (int index ) {
252
+ currentIndex = index ;
253
+ }
254
+
250
255
@ Override
251
256
public void onDataChange (DataSnapshot snapshot ) {
252
257
String key = snapshot .getKey ();
0 commit comments