Hello Friends, Today we will teach you How to add and delete data from recyclerview in android.
So Follow the Steps.
Step 1. :- Open build.gradle (Module: app) and add RecyclerView's Dependency
For API Level 28 use:-
implementation 'com.android.support:recyclerview-v7:28.0.0'
For API Level 29 (AndroidX) use:-
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
Step 2. :- Now Open your XML layout file and add RecyclerView Element.
activity_main.xml :-
Note:- We are using androidX because we currently use API Level 29
<?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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</RelativeLayout>
Note:- We are using androidX because we currently use API Level 29
Step 3. :- Now we need to create a XML layout file for our RecyclerView as a row_layout.xml
Design
row_layout.xml :-
Design
row_layout.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="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/white">
<ImageView
android:id="@+id/image_view"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="6dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/image_view"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/version_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name_text_view"
android:layout_marginLeft="5dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/image_view"
android:text="1.0"
android:textSize="14sp" />
<ImageView
android:id="@+id/copy_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:src="@drawable/ic_copy"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_delete"
android:layout_below="@+id/copy_image"
android:id="@+id/delete_image"
android:layout_alignEnd="@+id/version_text_view"
android:layout_alignParentEnd="true"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Step 4. :- Now we need to Create a Data Model class
DataModel.java :-
package com.akshayarora.myapplication;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
public class DataModel {
private String name,version;
private int imageId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
@NonNull
@Override
public String toString() {
return name;
}
public static List<DataModel> getObjectList(){
List<DataModel> dataList = new ArrayList<>();
int[] images = getImages();
String[] names = getNames();
String[] versions = getVersions();
for (int i = 0; i <images.length; i++){
DataModel model = new DataModel();
model.setImageId(images[i]);
model.setName(names[i]);
model.setVersion(versions[i]);
dataList.add(model);
}
return dataList;
}
private static int[] getImages(){
int[] images = {R.drawable.cupcake, R.drawable.donut, R.drawable.eclair, R.drawable.froyo,
R.drawable.gingerbread, R.drawable.honeycomb, R.drawable.icecreamsandwich,
R.drawable.jellybean, R.drawable.kitkat, R.drawable.lollipop, R.drawable.marshmallow,
R.drawable.nougat, R.drawable.oreo, R.drawable.pic, R.drawable.q};
return images;
}
private static String[] getNames(){
String[] names = {"CupCake", "Donut", "Eclair", "Froyo", "GingerBread", "HoneyComb",
"Ice Cream Sandwich", "Jelly Bean", "Kitkat", "Lollipop", "Marshmallow", "Nougat", "Oreo",
"Pie", "Android 10"};
return names;
}
private static String[] getVersions(){
String[] versions = {"1.5", "1.6", "2.0 - 2.1", "2.2 - 2.2.3", "2.3 - 2.3.7",
"3.0 - 3.2.6", "4.0 - 4.0.4", "4.1 - 4.3.1", "4.4 - 4.4.4", "5.0 - 5.1.1", "6.0 - 6.0.1",
"7.0 - 7.1.2", "8.0 - 8.1", "9.0", "10.0"};
return versions;
}
}
We defined 3 variable for image,name and version.Now we need to create another method getObjectList(). This object list will return the dataList which will have the list of the image and the title of the each item and version of the each item.
Step 5. :- Now need to create MyAdapter class
MyAdapter.java :-
package com.akshayarora.myapplication;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<DataModel> dataModels;
private LayoutInflater inflater;
public MyAdapter(List<DataModel> dataModels,Context context) {
this.dataModels = dataModels;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.row_layout,parent,false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
DataModel model = dataModels.get(position);
holder.setData(model,position);
holder.setListeners();
}
@Override
public int getItemCount() {
return dataModels.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView name,version;
private ImageView imageView,imgDelete,imgCopy;
private DataModel currentObject;
private int position;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.name_text_view);
version = (TextView)itemView.findViewById(R.id.version_text_view);
imageView = (ImageView)itemView.findViewById(R.id.image_view);
imgCopy = (ImageView)itemView.findViewById(R.id.copy_image);
imgDelete = (ImageView)itemView.findViewById(R.id.delete_image);
}
public void setData(DataModel currentObject, int position){
this.name.setText(currentObject.getName());
this.imageView.setImageResource(currentObject.getImageId());
this.position = position;
this.currentObject = currentObject;
}
public void setListeners() {
imgDelete.setOnClickListener(MyViewHolder.this);
imgCopy.setOnClickListener(MyViewHolder.this);
imageView.setOnClickListener(MyViewHolder.this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.delete_image:
removeItem(position);
Toast.makeText(inflater.getContext(), dataModels.get(position)+" Item Deleted", Toast.LENGTH_SHORT).show();
break;
case R.id.copy_image:
addItem(position,currentObject);
Toast.makeText(inflater.getContext(), dataModels.get(position)+" Item Added", Toast.LENGTH_SHORT).show();
break;
}
}
}
public void removeItem(int position){
dataModels.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,dataModels.size());
}
public void addItem(int position, DataModel currentObject){
dataModels.add(position,currentObject);
notifyItemInserted(position);
notifyItemRangeChanged(position,dataModels.size());
}
}
In our MyAdapter Class we create 2 new method one for removeItem and second for addItem in the recyclerview.
We implement OnClickListener in our MyViewHolder class for set Listener on Delete and Copy image.
In removeItem method we take position of the item of the recyclerview for delete the row.
notifyItemRemoved this function notify the adapter that there is the change made in this position and adapter updated it's self automatically.
Step 6. :- Now open MainActivity.java and make some changes
MainActivity.java :-
package com.akshayarora.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyAdapter(DataModel.getObjectList(),this);
recyclerView.setAdapter(myAdapter);
}
}
Now run the project and see the output
Output :-
Enjoy !







No comments:
Post a Comment
Note: Only a member of this blog may post a comment.