我正在像下面的代码那样从Firebase数据库中获取数据数组,然后将一个项目添加到该数组列表中
FirebaseDatabase.getInstance().getReference().child("CategoryWallpaper").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
GenericTypeIndicator<List<WallpaperItem>> t = new GenericTypeIndicator<List<WallpaperItem>>(){};
List<WallpaperItem> yourStringArray = dataSnapshot.getValue(t);
WallpaperItem wallpaperItem = new WallpaperItem();
wallpaperItem.setCategoryId("Love");
wallpaperItem.setViewCount(-200);
wallpaperItem.setImageLink("https://www.google.com");
yourStringArray.add(wallpaperItem);
FirebaseDatabase.getInstance().getReference().child("CategoryWallpaper").setValue(yourStringArray, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
我的数据库如下所示
将项目添加到数组列表后,我已经在调试模式下完成了代码检查,我看到了以下结果-该记录已添加到580的ArrayList中
但是我在下面一行出现错误
FirebaseDatabase.getInstance().getReference().child("CategoryWallpaper").setValue(yourStringArray
错误如下所示
com.google.firebase.database.DatabaseException: Failed to parse node with class class .MainActivity$2$3
at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(com.google.firebase:firebase-database@@16.0.6:103)
at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(com.google.firebase:firebase-database@@16.0.6:28)
at com.google.firebase.database.snapshot.PriorityUtilities.parsePriority(com.google.firebase:firebase-database@@16.0.6:39)
at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@16.0.6:207)
at MainActivity$2.onDataChange(MainActivity.java:151)