android - 使用 Android 更新 Firebase 中的数据
问题描述
我在 Firebase 中有一个这样的 数据库
所以,我想在用户点击收藏时更改收藏值。我尝试了一些代码,但它不会改变最喜欢的值。
private void uploadSetFavorite(final boolean isSetFavorite) {
reference.child(Common.FIREBASE_MUSCLE_EXERCISE_CHEST_TABLE).child(Common.EXERCISE_SET_FAVORITE_PROPERTY).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ExerciseMuscleDetail muscleDetail = snapshot.getValue(ExerciseMuscleDetail.class);
muscleDetail.setFavorite(isSetFavorite);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
当我调试时,dataSnapshot 显示 {key = favorite, value = null}。有人可以帮助我吗?
解决方案
请尝试类似:
HashMap<String, Object> params = new HashMap<>();
params.put("favourite", true/false); // with true/false use your desired value
yourDatabaseChildReference.updateChildren(params);
// yourDatabaseChildReference will be your dbName -> table name (chest) -> item name (1 or 2 or so on)
推荐阅读
- python - ImportError:无法从“object_detection.meta_architectures”导入名称“deepmac_meta_arch”
- c++ - “pthread_join”不会在刚刚取消的线程上返回(使用“pthread_cancel”)
- c# - 同一命令上的 Npgsql 二进制导入器和数据读取器
- sql - Spark SQL 不能减少 bigint 数组
- python - 如何在 tkinter python 中停止线程
- javascript - React Router 没有重定向到正确的页面,重定向到的页面是空的。没有错误
- http - 是否可以通过向 localhost 快速发送大量 HTTP 请求来耗尽您的动态端口范围?
- python - 计算源 IP 向目标 IP 发送数据包的次数
- java - 如何在kotlin中将功能从片段显示到小部件?
- laravel - 如何从类别表中获取 5 个类别并在 laravel 中获取每个类别的 4 个帖子