我想从我的数据库中获取所有孩子。我试过像Google开发者发布的YouTube上几乎类似的代码,但从那时起就有了改变。我只想获取用户数据以下的所有信息(有用户的配置文件)。 我的数据库看起来像:
我希望得到 userdata
下的所有信息
这是我的代码。
FirebaseDatabase database5 = FirebaseDatabase.getInstance();
DatabaseReference myRef5 = database5.getReference("userdata");
myRef5.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
Map<String, String> map = dataSnapshot.getValue(Map.class);
map.getClass();
Log.i("Mufz", "Value is: " + map.getClass());
// Toast.makeText(getApplicationContext(), `value3.toString(), Toast.LENGTH_SHORT).show();`
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("TAG", "Failed to read value.", error.toException());
// Toast.makeText(getApplicationContext(), error.toException().toString(), Toast.LENGTH_SHORT).show();
}
});
运行之后没有任何反应,它会运行prevoroius活动或出现错误
08-16 19:29:47.225 32507-32507/com.samo.facedatefb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samo.facedatefb, PID: 32507
com.google.firebase.database.DatabaseException: Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead
at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
at com.google.android.gms.internal.zzaln.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.samo.facedatefb.MapsActivity$3.onDataChange(MapsActivity.java:306)
at com.google.android.gms.internal.zzaie.zza(Unknown Source)
at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
哪里可能有问题?感谢。
答案 0 :(得分:0)
更改
Map<String, String> map = dataSnapshot.getValue(Map.class);
要
Map<String, Object> map = dataSnapshot.getValue(Map.class);
答案 1 :(得分:0)
使用GenericTypeIndicator
类来封送数据。
根据文件:
由于Java实现泛型(类型擦除)的方式,它是 必须使用稍微复杂的方法来正确使用 在运行时解析泛型集合的类型。
而不是:
Map<String, String> map = dataSnapshot.getValue(Map.class);
使用GenericTypeIndicator:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
GenericTypeIndicator<Map<String, Object>> m = new GenericTypeIndicator<Map<String, Object>>(){};
Map<String, Object> map = snapshot.getValue(m);
long age = (long)map.getValue("age");
Log.i("Mufz", "User Data Value is: " + snapshot.getKey());
Log.i("Mufz", "User Name Value is: " + map.getValue("username"));
Log.i("Mufz", "User Age Value is: " + age);
}
}
答案 2 :(得分:0)
据我所知,DataSnapshot
会为您提供array
而不是object
。那么也许你可以尝试这样的事情:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
YourObject obj = snapshot.getValue(YourObject.class);
}
}