12. Android Recycler View: How to Insert, Update and Delete Item?

 

1. Manifest 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/contactsvg"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.RecyclerviewExample"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

2. Java 

1. MainActivity.java:  

package com.example.recyclerviewexample;

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

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclertxt;
RecyclerContactAdapter adapter;
FloatingActionButton btnOpenDialog;
ArrayList<ContactModel> arrContact = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclertxt = findViewById(R.id.recyclertxt);
btnOpenDialog = findViewById(R.id.btnOpenDialog);

btnOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.add_update_layout);
EditText edtName = dialog.findViewById(R.id.edtName);
EditText edtNumber = dialog.findViewById(R.id.edtNumber);
Button btnAction = dialog.findViewById(R.id.btnAction);

btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = "", number = "";
if(!edtName.getText().toString().equals("")) {
name = edtName.getText().toString();
} else {
Toast.makeText(MainActivity.this, "Please Enter Contact Name!", Toast.LENGTH_SHORT).show();
}
if (!edtNumber.getText().toString().equals("")){
number = edtNumber.getText().toString();
}else {
Toast.makeText(MainActivity.this, "Enter a valid number!", Toast.LENGTH_SHORT).show();
}
arrContact.add(new ContactModel(R.drawable.d, name, number));
adapter.notifyItemInserted(arrContact.size()-1);
recyclertxt.scrollToPosition(arrContact.size()-1);
dialog.dismiss();

}
});
dialog.show();
}
});


//Yeh hum tb use krty hain jb hum aik value ko bar bar istimal krna ho
// ContactModel model = new ContactModel(R.drawable.a, "Ali", "03041139358");
arrContact.add(new ContactModel(R.drawable.a, "Ali", "03041139358"));
arrContact.add(new ContactModel(R.drawable.b, "Aslam", "030411334454"));
arrContact.add(new ContactModel(R.drawable.c, "Haider", "03041139358"));
arrContact.add(new ContactModel(R.drawable.d, "Noman", "03041139358"));
arrContact.add(new ContactModel(R.drawable.a, "Ali", "03041139358"));
arrContact.add(new ContactModel(R.drawable.b, "Aslam", "030411334454"));
arrContact.add(new ContactModel(R.drawable.c, "Haider", "03041139358"));
arrContact.add(new ContactModel(R.drawable.d, "Noman", "03041139358"));
arrContact.add(new ContactModel(R.drawable.a, "Ali", "03041139358"));
arrContact.add(new ContactModel(R.drawable.b, "Aslam", "030411334454"));
arrContact.add(new ContactModel(R.drawable.c, "Haider", "03041139358"));
arrContact.add(new ContactModel(R.drawable.d, "Noman", "03041139358"));

recyclertxt.setLayoutManager(new LinearLayoutManager(this) );
adapter = new RecyclerContactAdapter(this, arrContact);
recyclertxt.setAdapter(adapter);
}
}

2. RecyclerContactAdapter.java: 

package com.example.recyclerviewexample;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> {

ArrayList<ContactModel> arrContacts;
Context context;
RecyclerContactAdapter(Context context, ArrayList<ContactModel> arrContacts){
this.context = context;
this.arrContacts = arrContacts;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.contact_row, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position){

ContactModel model1 = (ContactModel) arrContacts.get(position);

holder.imgContact.setImageResource(model1.img);
holder.txtName.setText(model1.name);
holder.txtNumber.setText(model1.number);

holder.llRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.add_update_layout);

EditText edtName = dialog.findViewById(R.id.edtName);
EditText edtNumber = dialog.findViewById(R.id.edtNumber);
Button btnAction = dialog.findViewById(R.id.btnAction);
TextView txtTitle = dialog.findViewById(R.id.txtTitle);

btnAction.setText("Update");
txtTitle.setText("Update Contact");
edtName.setText(arrContacts.get(position).name);
edtNumber.setText(arrContacts.get(position).number);

btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name="", number="";
if(!edtName.getText().toString().equals("")) {
name = edtName.getText().toString();
} else {
Toast.makeText(context, "Please Enter Contact Name!", Toast.LENGTH_SHORT).show();
}
if (!edtNumber.getText().toString().equals("")){
number = edtNumber.getText().toString();
}else {
Toast.makeText(context, "Enter a valid number!", Toast.LENGTH_SHORT).show();
}
arrContacts.set(position, new ContactModel(arrContacts.get(position).img, name, number));
notifyItemChanged(position);

dialog.dismiss();
}
});

dialog.show();
}
});
holder.llRow.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle("Delete Contact")
.setMessage("Are you sure want to delete")
.setIcon(R.drawable.baseline_delete_24)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
arrContacts.remove(position);
notifyItemRemoved(position);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.show();
return true;
}
});
}

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

public class ViewHolder extends RecyclerView.ViewHolder{

TextView txtName, txtNumber;
ImageView imgContact;
LinearLayout llRow;
public ViewHolder(View itemView){
super(itemView);

txtName = itemView.findViewById(R.id.txtName1);
txtNumber = itemView.findViewById(R.id.txtName2);
imgContact = itemView.findViewById(R.id.imgContact);
llRow = itemView.findViewById(R.id.llRow);
}
}
}

3. ContactModel.java

package com.example.recyclerviewexample;

public class ContactModel {
int img;
String name, number;
public ContactModel(int img, String Name, String Number){
this.img = img;
this.name = Name;
this.number = Number;
}

public ContactModel(String name, String number){
this.name = name;
this.number = number;
}
}

3. Xml

1. Activity_main.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: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/recyclertxt"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:tint="#fff"
android:layout_alignParentBottom="true"
android:id="@+id/btnOpenDialog"
android:src="@drawable/contactsvg"
android:layout_margin="21dp"/>
</RelativeLayout>

2. contact_row.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="7dp"
app:cardUseCompatPadding="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="@+id/llRow"
android:padding="11dp">
<ImageView
android:id="@+id/imgContact"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#C6F492"
android:contentDescription="Contact Image"
android:backgroundTint="#B9B4BA"
tools:src="@drawable/contactsvg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact"
android:textSize="22sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txtName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Number"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

3. add_update_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#73D6E3"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:paddingBottom="11dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#fff"
android:id="@+id/txtTitle"
android:layout_margin="21dp"
android:text="Add Contact"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="11dp"
android:id="@+id/edtName"
android:hint="Enter Name"
android:textSize="48dp"
android:layout_marginBottom="11dp"
android:layout_marginRight="11dp"
android:inputType="text"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="11dp"
android:id="@+id/edtNumber"
android:hint="Enter Number"
android:textSize="48dp"
android:layout_marginBottom="11dp"
android:layout_marginRight="11dp"
android:inputType="number"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/btnAction"
android:backgroundTint="#fff"
android:textColor="#73D6E3"
android:layout_gravity="end"/>

</LinearLayout>



Post a Comment (0)
Previous Post Next Post