如何从Firebase android获取所有的子列表

我想从Android中的Firebase的所有孩子列表。

我已经实现这个代码,但它不工作。

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) { } }); 

我希望下面的代码工作

 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以这种格式存储一系列值:

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

如果这是你的方式,你会得到错误的types。 上面的结构被表示为一个Map ,而不是一个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) { } }); 

希望它有帮助!

我做了这样的事情:

 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) { } }); 

正如弗兰克所说, Firebase"key": "Value"格式存储值序列"key": "Value" ,这是一个地图结构

从这个序列中获得List

  1. String的 HashMap和你的对象初始化GenericTypeIndicator
  2. DataSnapShot的值作为GenericTypeIndicator获取Map中
  3. HashMap值初始化ArrayList

 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()); 

为我工作很好,希望它有帮助。

将数据保存到Firebase 从版本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; } } } 

在我的情况下只有给定的解决scheme工作正常

Firebase ArrayList结构的屏幕截图:

在这里输入图像描述

如何从DataSnapshotFirebase 获取整个列表

 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 🙁模型类)。

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

希望这可以正常工作。