如何从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
- 用String的 HashMap和你的对象初始化GenericTypeIndicator 。
- 将DataSnapShot的值作为GenericTypeIndicator获取到Map中 。
- 用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结构的屏幕截图:
如何从DataSnapshot从Firebase 获取整个列表 。
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; }
希望这可以正常工作。
- 如何获得一个不可滚动的ListView?
- 如何使用onSaveInstanceState()和onRestoreInstanceState()?
- android studio在哪里构build我的.apk文件?
- android:获取图像尺寸而不打开它
- 安装APK时出现DELETE_FAILED_INTERNAL_ERROR错误
- 从GoogleMap中移除一个标记
- ObjectAnimatoranimationLinearLayout宽度
- Android N Java 8function(Jack编译器)和Kotlin互操作
- 如何在Android Studio中使用Google Maps Api v2创buildAndroid应用程序?