Tutorial for recycleview adapter in android with java as its programming language in android 10

Introduction
Adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like as list view, spinner etc.

Create New Application with name recycleviewpost with empty Activity

Creating the activity_main.xml layout ( code with explanation )

Below is the code for activity_main.xml layout.

<?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:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />

</RelativeLayout>


Creating the user_row.xml layout ( code with explanation )

Let’s create a user_row layout which will hold the two TextViews and will be used to show name and address.

Below is the code for user_row.xml layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000" />

<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000" />

</LinearLayout>


Creating the adapter ( code with explanation )
In adapter we have to create a public class which extends RecyclerView.Adapter<MyAdapter.MyViewHolder> which is used to create a view holder for our adapter. In view holder we have to write the code for our UI component. In onCreateViewHolder() method we have to write the code for our UI component. In onBindViewHolder() method we have to write the code for setting data in UI component. In getItemCount() we have to return the count of item in the data source.
Example:
Let’s create a simple example to use RecyclerView with LinearLayoutManager which shows how to use RecyclerView with LinearLayoutManager.


Below is the code for custom adapter.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private Context context;
private List<User> userList;

public MyAdapter(Context context, List<User> userList) {
this.context = context;
this.userList = userList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
User user = userList.get(position);
holder.name.setText(user.getName());
holder.address.setText(user.getAddress());
}

@Override
public int getItemCount() {
return userList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

public TextView name, address;

public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
address = (TextView) itemView.findViewById(R.id.address);
}
}
}

 

3. Creating the model
First of all let’s create a model class which will hold the data.

Creating the User
class ( code with explanation )
Below is the code for model class.

public class User {

private String name;
private String address;

public User(String name, String address) {
this.name = name;
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}


Creating the main activity ( code with explanation )

First of all let’s create a main activity which will hold the RecyclerView in it. Also we will add a button in the main activity to add new item in the data source.

Creating the MainActivity

Below is the code for main activity.

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;

private MyAdapter adapter;
private List<User> userList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

userList = new ArrayList<>();
adapter = new MyAdapter(this, userList);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

prepareUserData();
}

private void prepareUserData() {
User user = new User("John Doe", "john@gmail.com");
userList.add(user);

user = new User("Will Smith", "will@gmail.com");
userList.add(user);

user = new User("Brad Pitt", "brad@gmail.com");
userList.add(user);

user = new User("Tom Cruise", "tom@gmail.com");
userList.add(user);

user = new User("George Clooney", "george@gmail.com");
userList.add(user);

user = new User("Leonardo DiCaprio", "leonardo@gmail.com");
userList.add(user);

adapter.notifyDataSetChanged();
}
}

 

Output
When we run above example using android emulator we will get following result.

 

Source Code is available at github at this link

 

 




Taher Ali Badnawarwala

Taher Ali, drives to create something special, He loves swimming ,family and AI from depth of his heart . He loves to write and make videos about AI and its usage


Leave a Comment


No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *