Android RecyclerView Example With Model Class
1/ Create A New Project add xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2/ Add a xml for item card_lay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="@color/gray"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Name" />
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="price" />
<TextView
android:id="@+id/dsc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dsc" />
</LinearLayout>
</RelativeLayout>
3/ Crate Model Class DataModel.Class
package com.example.conditiontest;
public class DataModel {
String bookName;
int bookPrice;
String bookDetails;
public DataModel(String bookName, int bookPrice, String bookDetails) {
this.bookName = bookName;
this.bookPrice = bookPrice;
this.bookDetails = bookDetails;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookPrice() {
return bookPrice;
}
public void setBookPrice(int bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookDetails() {
return bookDetails;
}
public void setBookDetails(String bookDetails) {
this.bookDetails = bookDetails;
}
}
4/ Add an Adapter Class CustomAdapter.class
package com.example.conditiontest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
ArrayList<DataModel> bookData;
Context context;
public CustomAdapter(Context context, ArrayList<DataModel> bookData) {
this.context = context;
this.bookData = bookData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_lay, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.name.setText(bookData.get(position).bookName.toString());
holder.price.setText(bookData.get(position).bookPrice+"");
holder.dsc.setText(bookData.get(position).bookDetails+"");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, bookData.get(position).bookName.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return bookData.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView price;
TextView dsc;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
price = (TextView) itemView.findViewById(R.id.price);
dsc = (TextView) itemView.findViewById(R.id.dsc);
}
}
}
5/ Add a array in MainAcitivity.class
package com.example.conditiontest;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<DataModel> bookList = new ArrayList<DataModel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bookList.add(new DataModel("Java Book",500,"This is Java Book"));
bookList.add(new DataModel("Kotlin Book",600,"This is Kotlin Book"));
bookList.add(new DataModel("Jetpack Book",700,"This is Jetpack Book"));
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
CustomAdapter customAdapter=new CustomAdapter(MainActivity.this,bookList);
recyclerView.setAdapter(customAdapter);
}
}