Open In App

How to Update RecyclerView Adapter Data in Android?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

RecyclerView is used in many android applications to display the list of data within android applications. We can dynamically add or remove data from our recycler view. For updating data in the RecyclerView, we have to use the Adapter class. Adapter handles all the data within our recycler view. In this article, we will take a look at How to Update RecyclerView Adapter Data to update our RecyclerView. A sample video is given at the end to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--edit text for adding new language-->
    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:hint="Add item"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--button to add new language-->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:text="Add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--recycler view for displaying data-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Create a layout file for the item of RecyclerView

Navigate to app > res > layout, right click and select, New > Layout Resource File and name it as item_rv and add the below code to it. Comments are added in the code to get to know in detail. 

item_rv.xml:

XML
<?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="4dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="9dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--text view for displaying a text-->
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="8dp"
            android:text="Language"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>


Step 4: Creating an Adapter class

Navigate to app > java > your app's package name, Right click on it, New > Kotlin class and name it as Adapter and add the below code to it. Comments are added in the code to get to know in detail. 

Adapter.java
package com.gtappdevelopers.googlemapsroutes.RecyclerView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.gtappdevelopers.googlemapsroutes.R;
import java.util.ArrayList;

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    private ArrayList<String> list;

    public Adapter(ArrayList<String> list, Context context) {
        this.list = list;
    }

    @NonNull
    @Override
    public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // this method is use to inflate the layout file 
        // which we have created for our recycler view.
        // on below line we are inflating our layout file.
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
        
        // at last we are returning our view holder
        // class with our item View File.
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
        // on below line we are setting text to our text view.
        holder.textView.setText(list.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        // on below line we are creating variable. 
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // on below line we are initialing our variable. 
            textView = itemView.findViewById(R.id.textView);
        }
    }
}
Adapter.kt
package org.geeksforgeeks.demo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class Adapter(
    // on below line we are passing variables
    // as course list and context
    private var list: ArrayList<String>,
) : RecyclerView.Adapter<Adapter.ViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        // this method is use to inflate the layout file
        // which we have created for our recycler view.
        // on below line we are inflating our layout file.
        val itemView = LayoutInflater.from(parent.context).inflate(
            R.layout.item_rv,
            parent, false
        )
        // at last we are returning our view holder
        // class with our item View File.
        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // on below line we are setting text to our text view.
        holder.textView.text = list[position]
    }

    override fun getItemCount(): Int {
        // on below line we are
        // returning the size of list.
        return list.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // on below line we are initializing our text view.
        val textView: TextView = itemView.findViewById(R.id.textView)
    }

}


Step 5: Working with the MainActivity file 

Navigate to app > java > your app's package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail. 

MainActivity.java
package com.gtappdevelopers.googlemapsroutes;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.gtappdevelopers.googlemapsroutes.RecyclerView.LanguageRVAdapter;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    // on below line we are creating variables.
    private RecyclerView rv;
    private EditText editText;
    private Button button;
    private ArrayList<String> list;
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // on below line we are initializing our variables.
        rv = findViewById(R.id.rv);
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);
        list = new ArrayList<>();

        // on below line we are adding data to our list.
        list.add("C++");
        list.add("C");
        list.add("Java");

        // on below line we are adding our list to our adapter.
        adapter = new Adapter(list);

        // on below line we are setting 
        // adapter to our recycler view.
        rv.setAdapter(adapter);

        // on below line we are adding click listener for our add button.
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are calling 
                // add item method.
                addItem(editText.getText.toString());
            }
        });
    }

    // on below line we are creating 
    // a new function to add item.
    private void addItem(String item) {
        // on below line we are checking
        // if item is empty or not.
        if (!item.isEmpty()) {
            // on below line we are adding
            // item to our list
            list.add(item);
            // on below line we are notifying 
            // adapter that data has updated.
            adapter.notifyDataSetChanged();
        }
    }

}
MainActivity.kt
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    // on below line we are creating variables.
    private lateinit var rv: RecyclerView
    private lateinit var editText: EditText
    private lateinit var button: Button
    private lateinit var list: ArrayList<String>
    private lateinit var adapter: Adapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // on below line we are initializing our variables.
        rv = findViewById(R.id.rv)
        editText = findViewById(R.id.editText)
        button = findViewById(R.id.button)
        list = ArrayList()

        // on below line we are
        // adding data to our list.
        list.plusAssign("C++")
        list.plusAssign("C")
        list.plusAssign("Java")

        // on below line we are adding our list to our adapter.
        adapter = Adapter(list = list)

        // on below line we are setting
        // adapter to our recycler view.
        rv.adapter = adapter

        // on below line we are adding click listener
        // for our add button.
        button.setOnClickListener {
            // on below line we are calling add item method.
            addItem(editText.text.toString())
        }
        // on below line we are notifying adapter
        // that data in adapter has been updated.
        adapter.notifyDataSetChanged()

    }

    // on below line we are creating a
    // new function to add item.
    private fun addItem(item: String) {
        // on below line we are checking
        // if item is empty or not.
        if (item.isNotEmpty()) {
            // on below line we are
            // adding item to our list
            list.plusAssign(item)
            // on below line we are notifying
            // adapter that data has updated.
            adapter.notifyDataSetChanged()
        }
    }
}


Output:


Similar Reads