How to Add and Remove Item from Android RecyclerView

In this android programming source code example, we are going to explore how to add and remove item in android RecyclerView .

When a RecyclerView is initialized and loaded with data, depending on use-case a user might want to remove or add data to the RecyclerView.

Below is a simple example on how to add or remove item from a RecyclerView.

activity_main.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_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:background="@color/colorAccent"
        android:layout_alignParentBottom="true"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:paddingLeft="16dp"
        android:onClick="aditem"
        android:paddingRight="16dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove Item"
        android:background="@color/colorAccent"
        android:layout_alignParentBottom="true"
        android:textColor="@color/white"
        android:onClick="removeitem"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textStyle="bold"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"/>

</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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


public class RecyclerViewActivity14 extends AppCompatActivity {

    private RecyclerView list;

    private adapter recyclerAdapter;

    ArrayList<String> country;

    int index = 0;

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

        //Data
        country = new ArrayList<>();
        country.add(String.valueOf(index));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        recyclerAdapter = new adapter(RecyclerViewActivity14.this, country);
        list.addItemDecoration(new DividerItemDecoration(list.getContext(), layoutManager.getOrientation()));
        list.setAdapter(recyclerAdapter);
    }

    public void aditem(View view){
        index++;
        country.add(index,String.valueOf(index));
        recyclerAdapter.notifyItemInserted(index);
    }

    public void removeitem(View view){
        index--;
        if(index+1>=0){
            country.remove(index+1);
            recyclerAdapter.notifyItemRemoved(index+1);
        }
    }

    private class adapter extends RecyclerView.Adapter<adapter.myViewHolder> {
        Context context;
        List<String> mData;

        public adapter(Context context, List<String> data) {
            this.context = context;
            this.mData = data;
        }

        @Override
        public adapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false);
            return new myViewHolder(view);
        }

        @Override
        public void onBindViewHolder(adapter.myViewHolder holder, int position) {
            holder.country.setText(mData.get(position));
        }

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

        public class myViewHolder extends RecyclerView.ViewHolder {
            TextView country;

            public myViewHolder(View itemView) {
                super(itemView);
                country = (TextView) itemView.findViewById(R.id.country);
            }
        }
    }
}

android remove recyclerview item

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.