Android Firebase读取数据内部的数据
在我的Kotlin应用程序中,我有一个Firebase,其值内有一组循环对象。如何读取中每个的值Android Firebase读取数据内部的数据,android,firebase,kotlin,firebase-realtime-database,Android,Firebase,Kotlin,Firebase Realtime Database,在我的Kotlin应用程序中,我有一个Firebase,其值内有一组循环对象。如何读取中每个的值 user -> history -> location for(dataSnapshot.children中的childsnapshot){ val name=childsnapshot.key val locList:Map=childsnapshot.value作为映射 val val_obj=JSONObject(locList)作为JSONObject val pin_im
user -> history -> location
for(dataSnapshot.children中的childsnapshot){
val name=childsnapshot.key
val locList:Map=childsnapshot.value作为映射
val val_obj=JSONObject(locList)作为JSONObject
val pin_image=val_obj.get(“image”).toString()
val history_location=val_obj.getJSONObject(“历史”).getJSONObject(“位置”)
var i=0;
而(i要获取lastCheckedDate
属性的值,对于Java用户,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationRef = rootRef.child("Qwerr").child("history").child("location");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String lastCheckedDate = ds.child("lastCheckedDate").getValue(String.class);
Log.d("TAG", lastCheckedDate);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
locationRef.addListenerForSingleValueEvent(valueEventListener);
对于Kotlin用户:
val valueEventListener: ValueEventListener = object : ValueEventListener() {
fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.getChildren()) {
val lastCheckedDate: String = ds.child("lastCheckedDate").getValue(String::class.java)
Log.d("TAG", lastCheckedDate)
}
}
fun onCancelled(@NonNull databaseError: DatabaseError) {
Log.d("TAG", databaseError.getMessage()) //Don't ignore potential errors!
}
}
locationRef.addListenerForSingleValueEvent(valueEventListener)
logcat中的结果将是:
2020/09/14 13:20:51
2020/09/14 13:21:49
...
如果需要在latlng
节点下获取数据,则应在数据库树中更深入一步,并添加.child(“latlng”)
调用
编辑:
要获取latlng
节点下存在的纬度
和经度
属性的值,请使用以下代码:
val valueEventListener: ValueEventListener = object : ValueEventListener() {
fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.getChildren()) {
val latitude: Long = ds.child("latlng").child("latitude").getValue(Long::class.java)
val longitude: Long = ds.child("latlng").child("longitude").getValue(Long::class.java)
Log.d("TAG", latitude + ", " + longitude)
}
}
fun onCancelled(@NonNull databaseError: DatabaseError) {
Log.d("TAG", databaseError.getMessage()) //Don't ignore potential errors!
}
}
locationRef.addListenerForSingleValueEvent(valueEventListener)
我想读取“Qwrr”用户中的所有值,我可以读取直接值,但我不知道如何读取SDE history->LOCATION中的值正如我在回答中已经说过的,添加一个.child(“latlng”)调用。如果您在获取数据时遇到困难,请编辑您的问题并添加latlng结构作为屏幕截图。这是我为用户“Qwerr”val locList所做的完整数据快照:Map=dataSnapshot.value as Map val_obj=JSONObject(locList)作为JSONObject val image=val_obj.getJSONObject(“image”)val history\u location=val_obj.getJSONObject(“history”).getJSONObject(“location”)hisory->location有一个单独的键值对。我不知道如何从这一点开始阅读。没有必要使用JSONObject
。正如我已经问过你的,请编辑你的问题并将latlng结构添加为屏幕截图。正如我已经问过你两次,请编辑你的问题并将latlng结构添加为屏幕截图。