![]() |
VOOZH | about |
This is the Part 6 of "Build a Social Media App in Android Studio" tutorial, and we are going to cover the following functionalities in this article:
Step 1: Create a new layout resource file
Go to the app > res > layout > New > Layout Resource File and name the file as row_posts. Below is the code for the row_posts.xml file.
Step 2: Create a new java class
Refer to this article Create Classes in Android Studio and name the class as ModelPost. Below is the code for the ModelPost.java file. Created this class to initialize the key so that we can retrieve the value of the key later.
Similarly, create another java class and name the class as AdapterPosts. Below is the code for the AdapterPosts.java file.
Step 3: Working with the fragment_home.xml file
Navigate to the app > res > layout > fragment_home.xml and add the below code to that file. Below is the code for the fragment_home.xml file.
Step 4: Working with the HomeFragment.java file
This is how we will be retrieved blogs from Firebase. Every time data changes the value will change accordingly.
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference("Posts");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
posts.clear();
for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(),databaseError.getMessage(),Toast.LENGTH_LONG).show();
}
});
Step 5: Create 2 new empty activities and name the activities as PostLikedByActivity and PostDetailsActivity. Keep the file as it is.
Output:
Note: Please Add drawable items before running the Application
Below is the file structure after performing these operations: