VOOZH about

URL: https://www.geeksforgeeks.org/android/implement-delete-a-blog-post-functionality-in-social-media-android-app/

⇱ Implement Delete a Blog Post Functionality in Social Media Android App - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement Delete a Blog Post Functionality in Social Media Android App

Last Updated : 23 May, 2024

This is the Part 13 of "Build a Social Media App on Android Studio" tutorial, and we are going to cover the following functionalities in this article:

  • We are going to delete the Blog written by the user.
  • Blogs can only be deleted by the owner of blogs. In the top right of the blog, a button is there.
  • After clicking on that a popup will come in that Delete button will come. After clicking on Delete, the blog will be successfully deleted from the blogs.

Step By Step Implementation

Step 1: Working with the AdapterPosts.java file

We will get the node inside the "Posts" which we want to delete. We are directly deleting the snapshot reference value.

Query query= FirebaseDatabase.getInstance().getReference("Posts").orderByChild("ptime").equalTo(pid);
 query.addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
 for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
 dataSnapshot1.getRef().removeValue();//removing the value after getting reference
 }

 pd.dismiss();
 Toast.makeText(context,"Deleted Successfully",Toast.LENGTH_LONG).show();
 }

 @Override
 public void onCancelled(@NonNull DatabaseError databaseError) {

 }
 });

Below is the updated code for the AdapterPosts.java file.

Output:


 


 

Note: Please Add drawable items before running the Application


 

Below is the file structure after performing these operations:


 

👁 Image


 

Comment
Article Tags:
Article Tags:

Explore