从 Firebase 迭代 dataSnapshot.value hashmap 的值
Iterate Values of a dataSnapshot.value hashmap from a Firebase
我正在为摩托车创建一个“虚拟车库应用程序”,但我无法从我的 Firebase 中检索数据,我只能访问数据库中每辆摩托车的 HashMap。
这是我的数据库内容:
database
这是我尝试检索数据的代码:
code
这是我尝试放置数据的 ExampleItem()
对象:ExampleItem
是否有任何方法可以遍历 dataSnapshot.value HashMap 的值以便为每个字符串调用设置器?
Is there any way to iterate through the values of the dataSnapshot.value HashMap in order to call the setters for each string?
您可以更简单地访问您需要的 属性。例如,要显示“motoBrand”的值属性,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mototrackRef = rootRef.child("mototrack");
mototrackRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String motoBrand = ds.child("motoBrand").getValue(String.class);
Log.d("TAG", motoBrand);
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
您可以用完全相同的方式获取其他属性的值。在科特林中看起来像这样:
val rootRef = FirebaseDatabase.getInstance().reference
val mototrackRef = rootRef.child("mototrack")
mototrackRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
for (ds in task.result.getChildren()) {
val motoBrand = ds.child("motoBrand").getValue(String::class.java)
Log.d("TAG", motoBrand)
}
} else {
Log.d("TAG", task.exception.getMessage()) //Don't ignore potential errors!
}
}
我正在为摩托车创建一个“虚拟车库应用程序”,但我无法从我的 Firebase 中检索数据,我只能访问数据库中每辆摩托车的 HashMap。 这是我的数据库内容:
database
这是我尝试检索数据的代码: code
这是我尝试放置数据的 ExampleItem()
对象:ExampleItem
是否有任何方法可以遍历 dataSnapshot.value HashMap 的值以便为每个字符串调用设置器?
Is there any way to iterate through the values of the dataSnapshot.value HashMap in order to call the setters for each string?
您可以更简单地访问您需要的 属性。例如,要显示“motoBrand”的值属性,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mototrackRef = rootRef.child("mototrack");
mototrackRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String motoBrand = ds.child("motoBrand").getValue(String.class);
Log.d("TAG", motoBrand);
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
您可以用完全相同的方式获取其他属性的值。在科特林中看起来像这样:
val rootRef = FirebaseDatabase.getInstance().reference
val mototrackRef = rootRef.child("mototrack")
mototrackRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
for (ds in task.result.getChildren()) {
val motoBrand = ds.child("motoBrand").getValue(String::class.java)
Log.d("TAG", motoBrand)
}
} else {
Log.d("TAG", task.exception.getMessage()) //Don't ignore potential errors!
}
}