In Android development with Volley Library, extracting data from JSON Arrays is a crucial skill. Let's delve into the process of extracting data from a JSON Array and displaying it in an Android application.
JSON Array is a collection of data that contains multiple JSON Objects sharing similar data structures. Identified by opening "[" and closing "]" braces, a JSON Array comprises several JSON Objects, each storing data in key-value pairs.
In this project, we will create a simple application featuring CardViews that display various courses available on Geeks for Geeks. The application will be developed using the Java language.
Below is a sample GIF illustrating the project:
Our Android app will showcase data extracted from the following JSON Array:
Course Name | Course Image | Course Mode | Course Tracks |
---|---|---|---|
Fork CPP | ![]() | Online Batch | 6 Tracks |
Linux & Shell Scripting Foundation | ![]() | Online Batch | 6 Tracks |
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
ProgressBar android:id="@ id/idPB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> |
androidx.recyclerview.widget.RecyclerView android:id="@ id/idRVCourses" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> |
Begin by navigating to the following path in your project directory: app > java > your app’s package name.
Right-click on your app’s package name and select New > Java Class.
Name the new Java class as CourseModal.
Add the provided code snippet below inside the CourseModal class:
Define variables for the course, including courseName
, courseimg
, courseMode
, and courseTracks
.
Create a constructor to initialize these variables.
Implement getter and setter methods for each variable.
The provided code snippet:
public class CourseModal {
// variables for our course
private String courseName;
private String courseimg;
private String courseMode;
private String courseTracks;
// creating constructor for our variables.
CourseModal(String courseName, String courseimg, String courseMode, String courseTracks) {
this.courseName = courseName;
this.courseimg = courseimg;
this.courseMode = courseMode;
this.courseTracks = courseTracks;
}
// creating getter and setter methods.
String getCourseName() {
return courseName;
}
void setCourseName(String courseName) {
this.courseName = courseName;
Step 6: Creating a layout file for each item of our RecyclerView.
<?xml version="1.0" encoding="utf-8"?> |
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:elevation="8dp" app:cardCornerRadius="8dp"> |
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> |
<!-- ImageView to display our course image --> |
<ImageView android:id="@id/idIVCourse" android:layout_width="match_parent" android:layout_height="300dp" android:layout_margin="5dp" /> |
<!-- Text view for our course name --> |
<TextView android:id="@id/idTVCourseName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:padding="5dp" android:text="Course Name" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> |
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal" android:weightSum="2"> |
<!-- Text view for our batch name --> |
<TextView android:id="@id/idTVBatch" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dp" android:text="Batch" android:textColor="@color/black" /> |
<!-- Text view to display course tracks --> |
<TextView android:id="@id/idTVTracks" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dp" android:text="Tracks" android:textColor="@color/black" /> |
</LinearLayout> |
</LinearLayout> |
</androidx.cardview.widget.CardView> |
Create an Adapter class to set data for the RecyclerView item.
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.squareup.picasso.Picasso; import java.util.ArrayList; public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.ViewHolder> { // Declaration of variables private ArrayList<CourseModal> courseModalArrayList; private Context context; // Constructor public CourseAdapter(ArrayList<CourseModal> courseModalArrayList, Context context) { this.courseModalArrayList = courseModalArrayList; this.context = context; } @NonNull @Override public CourseAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Inflate layout View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_rv_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull CourseAdapter.ViewHolder holder, int position) { // Bind data to views CourseModal modal = courseModalArrayList.get(position); holder.courseNameTV.setText(modal.getCourseName()); holder.courseTracksTV.setText(modal.getCourseTracks()); holder.courseModeTV.setText(modal.getCourseMode()); Picasso.get().load(modal.getCourseimg()).into(holder.courseIV); } @Override public int getItemCount() { return courseModalArrayList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { // Declaration of views private TextView courseNameTV, courseModeTV, courseTracksTV; private ImageView courseIV; public ViewHolder(@NonNull View itemView) { super(itemView); // Initialize views courseNameTV = itemView.findViewById(R.id.idTVCourseName); courseModeTV = itemView.findViewById(R.id.idTVBatch); courseTracksTV = itemView.findViewById(R.id.idTVTracks); courseIV = itemView.findViewById(R.id.idIVCourse); } } } |
In this step, you will be working with the MainActivity.java file.
Ready to embark on an exciting journey into the world of Android Development with Kotlin? It's time to make a change and dive into a fantastic learning experience with our self-paced course.