2016-07-01 34 views

risposta

6

Firebase non è progettato per essere ordinato in base a un indice visibile, ma piuttosto alle voci nel database when queried.

orderByChild, orderByKey, o orderByValue

Quindi, se si desidera riordinare i valori in Firebase, si dovrebbe dare ogni elemento un valore di 'indice' e chiamare orderByValue('index')


Poche righe di codice è possibile aggiornare gli elementi del database corrente per utilizzare questo indice:

int index = 0; 
mDatabase.child("data").addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 
     Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey()); 

     // A new comment has been added, add it to the displayed list 
     Data data = dataSnapshot.getValue(Data.class); 
     index++; 
     Map<String, Object> childUpdates = new HashMap<>(); 
     childUpdates.put("/data/" + dataSnapshot.getKey()+"/index", index); 
     mDatabase.updateChildren(childUpdates); 
    } 
+0

ok. Come potrei essere in grado di scorrere ogni elemento nel database e modificarne il valore per indice? – Smeekheff

+0

È possibile "aggiornare" gli elementi del database per includere un altro campo. –

+0

vero ma come potrei scorrere tutti gli elementi puoi fornire del codice? – Smeekheff