首页 > 解决方案 > 使用 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}。有人可以帮助我吗?

标签: androidfirebasefirebase-realtime-database

解决方案


请尝试类似:

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)

推荐阅读