DEV Community

Hyunjin Cho
Hyunjin Cho

Posted on

Android Studio/Firebase(1)

Structure
root

-- Comments

-- movidID

-- commentID

-- Playlist

-- userID

-- playlistID

//rootNode
FirebaseDatabase rootNode = FirebaseDatabase.getInstance();
//reference
DatabaseReference reference;
Enter fullscreen mode Exit fullscreen mode

// When adding one movie to the playlist

reference = rootNode.getReference("Playlist").child(userID).push();

Playlist playlist = new Playlist(id, userID, img_path, playRef.getKey());

reference .setValue(playlist).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {
                    // work of button for adding and removing
                    btn_playlist.setVisibility(View.INVISIBLE);
                    btn_playlist.setEnabled(false);
                    btn_remove.setVisibility(View.VISIBLE);
                    btn_remove.setEnabled(true);
                }
            })
Enter fullscreen mode Exit fullscreen mode

// When removing one movie from the playlist

DatabaseReference playcheckref = rootNode.getReference().child("Playlist").child(userID);

playcheckref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot : snapshot.getChildren()) {

// if there is the clicked movie in the list                   if(Objects.requireNonNull(dataSnapshot.getValue(Playlist.class)).getmID().equals(id)){
                        btn_playlist.setVisibility(View.INVISIBLE);
                        btn_playlist.setEnabled(false);
                        btn_remove.setVisibility(View.VISIBLE);
                        btn_remove.setEnabled(true);

// grab the playlist movie id
                        String vID = Objects.requireNonNull(dataSnapshot.getValue(Playlist.class)).getvID();
                        btn_remove.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                btn_playlist.setVisibility(View.VISIBLE);
                                btn_playlist.setEnabled(true);
                                btn_remove.setVisibility(View.INVISIBLE);
                                btn_remove.setEnabled(false);

                                DatabaseReference removeRef = rootNode.getReference("Playlist").child(userID).child(vID);
                                removeRef.setValue(null);

                                Toast.makeText(MovieDetail.this, "Successfully deleted" + Objects.requireNonNull(dataSnapshot.getValue(Playlist.class)).getvID(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    } else{
                        btn_playlist.setOnClickListener(addMovieToList);
                    }
                }
            }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)