Cómo obtener todas las listas de niños de Firebase android

Quiero toda la lista del niño de Firebase en androide.

Tengo implementar este código, pero no funciona.

mFirebaseRef = new Firebase(FIREBASE_URL); mFirebaseRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { List<String> td = (ArrayList<String>) dataSnapshot.getValue(); //notifyDataSetChanged(); } @Override public void onCancelled(FirebaseError firebaseError) { } }); 

Espero que bajo el código funciona

 Firebase ref = new Firebase(FIREBASE_URL); ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { Log.e("Count " ,""+snapshot.getChildrenCount()); for (DataSnapshot postSnapshot: snapshot.getChildren()) { <YourClass> post = postSnapshot.getValue(<YourClass>.class); Log.e("Get Data", post.<YourMethod>()); } } @Override public void onCancelled(FirebaseError firebaseError) { Log.e("The read failed: " ,firebaseError.getMessage()); } }); 

Firebase almacena una secuencia de valores en este formato:

 "-K-Y_Rhyxy9kfzIWw7Jq": "Value 1" "-K-Y_RqDV_zbNLPJYnOA": "Value 2" "-K-Y_SBoKvx6gAabUPDK": "Value 3" 

Si eso es lo que usted tiene, usted está recibiendo el tipo incorrecto. La estructura anterior se representa como un Map , no como una List :

 mFirebaseRef = new Firebase(FIREBASE_URL); mFirebaseRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue(); List<Object> values = td.values(); //notifyDataSetChanged(); } @Override public void onCancelled(FirebaseError firebaseError) { } }); 
 FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance(); DatabaseReference databaseReference = mFirebaseDatabase.getReference(FIREBASE_URL); databaseReference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) { Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node Log.v(TAG,""+ childDataSnapshot.child(--ENTER THE KEY NAME eg. firstname or email etc.--).getValue()); //gives the value for given keyname } } @Override public void onCancelled(DatabaseError databaseError) { } }); 

¡Espero eso ayude!

Hice algo como esto:

 Firebase ref = new Firebase(FIREBASE_URL); ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { Map<String, Object> objectMap = (HashMap<String, Object>) dataSnapshot.getValue(); List<Match> = new ArrayList<Match>(); for (Object obj : objectMap.values()) { if (obj instanceof Map) { Map<String, Object> mapObj = (Map<String, Object>) obj; Match match = new Match(); match.setSport((String) mapObj.get(Constants.SPORT)); match.setPlayingWith((String) mapObj.get(Constants.PLAYER)); list.add(match); } } } @Override public void onCancelled(FirebaseError firebaseError) { } }); 

Guardar y recuperar datos en – desde Firebase (ver depreciado 2.4.2)

 Firebase fb_parent = new Firebase("YOUR-FIREBASE-URL/"); Firebase fb_to_read = fb_parent.child("students/names"); Firebase fb_put_child = fb_to_read.push(); // REMEMBER THIS FOR PUSH METHOD //INSERT DATA TO STUDENT - NAMES I Use Push Method fb_put_child.setValue("Zacharia"); //OR fb_put_child.setValue(YOUR MODEL) fb_put_child.setValue("Joseph"); //OR fb_put_child.setValue(YOUR MODEL) fb_put_child.setValue("bla blaaa"); //OR fb_put_child.setValue(YOUR MODEL) //GET DATA FROM FIREBASE INTO ARRAYLIST fb_to_read.addValuesEventListener....{ public void onDataChange(DataSnapshot result){ List<String> lst = new ArrayList<String>(); // Result will be holded Here for(DataSnapshot dsp : result.getChildren()){ lst.add(String.valueOf(dsp.getKey())); //add result into array list } //NOW YOU HAVE ARRAYLIST WHICH HOLD RESULTS for(String data:lst){ Toast.make(context,data,Toast.LONG_LENGTH).show; } } } 

Como Frank dijo Firebase almacena secuencia de valores en el formato de "key": "Value" que es una estructura de mapa

Para obtener la lista de esta secuencia tiene que

  1. Inicialice GenericTypeIndicator con HashMap de String y su objeto .
  2. Obtenga el valor de DataSnapShot como GenericTypeIndicator en Map .
  3. Inicializar ArrayList con valores de HashMap .

 GenericTypeIndicator<HashMap<String, Object>> objectsGTypeInd = new GenericTypeIndicator<HashMap<String, Object>>() {}; Map<String, Object> objectHashMap = dataSnapShot.getValue(objectsGTypeInd); ArrayList<Object> objectArrayList = new ArrayList<Object>(objectHashMap.values()); 

Funciona bien para mí, Espero que ayude.

En mi caso sólo la solución dada funcionó bien.

Captura de pantalla de la estructura de FireBase ArrayList :

Introduzca aquí la descripción de la imagen

Cómo obtener toda la lista de Firebase de DataSnapshot .

 GenericTypeIndicator<Map<String, List<Education>>> genericTypeIndicator = new GenericTypeIndicator<Map<String, List<Education>>>() {}; Map<String, List<Education>> hashMap = dataSnapshot.getValue(genericTypeIndicator); for (Map.Entry<String,List<Education>> entry : hashMap.entrySet()) { List<Education> educations = entry.getValue(); for (Education education: educations){ Log.i(TAG, education.Degree); } } 

Education.java: (Clase del modelo).

 public class Education implements Serializable{ public String Degree; public String Result; } 

Espero que esto funciona bien.

  • La clase java.util.Map tiene parámetros genéricos de tipo, utilice GenericTypeIndicator en su lugar
  • ¿Cómo puedo usar una consulta Firebase para devolver un booleano?
  • No se puede obtener URI de referencia de almacenamiento en Firebase
  • Android: ¿Realmente necesito la autenticación personalizada de Firebase?
  • Firebase push notificación no funciona
  • Mi implementación de la indexación de aplicaciones no funciona
  • El perfil de FirebaseUser no se actualiza
  • Salir correctamente de un usuario de la aplicación Android
  • Cómo reordenar los datos de la base de datos en tiempo real firebase
  • Firebase-Android, No puede guardar datos después de mucho tiempo inactivo
  • Firebase "mientras se deserializaba, pero obtuvo una clase java.util.ArrayList"
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.