Firebase更新节点的所有子节点

时间:2017-12-30 08:51:01

标签: android firebase firebase-realtime-database

我正在尝试更新Firebase数据库中的单个节点,但我的方法是添加两个节点,一个用混音,一个用只有一个标题。如何仅更新播放列表中的孩子。

public void addToExistingPlaylist(String title) {
    Query playlistQuery = mDatabase.child("playlists/" + FirebaseAuth.getInstance().getCurrentUser().getUid()).orderByChild("playlistTitle").equalTo(title);
    playlistQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                Playlist playlist = childSnapshot.getValue(Playlist.class);
                ArrayList<Mix> mixes = new ArrayList<Mix>();
                if (playlist.getMixes() != null) {
                    mixes = playlist.getMixes();
                }
                mixes.add(((MusicDetailFragment)mDashboardDetailFragment).mSelectedTrack);
                playlist.setMixes(mixes);

                DatabaseReference playlistRef = mDatabase.child("playlists/" + FirebaseAuth.getInstance().getCurrentUser().getUid()); //save this mix under mixes --> userUid -- new song

                playlistRef.push().setValue(playlist).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Snackbar newPlayListAddedSnack;
                            newPlayListAddedSnack = Snackbar.make(mCoordinatorLayout, getString(R.string.song_added_to_existing_playlist), Snackbar.LENGTH_LONG);
                            newPlayListAddedSnack.show();
                        } else {
                            Snackbar errorSnack;
                            errorSnack = Snackbar.make(mCoordinatorLayout, getString(R.string.error_processing_request), Snackbar.LENGTH_LONG);
                            errorSnack.show();
                        }
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    alert.dismiss();
}

enter image description here

1 个答案:

答案 0 :(得分:0)

删除push()。您的代码应该是

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=C: \Users\g_jes\Documents\Gilberte Jessie\aspjProject\App_Data\Database.mdf;Integrated Security=True");