Android App Development With AndroidCource: Insert Update Delete View in recyclerview in android
Showing posts with label Insert Update Delete View in recyclerview in android. Show all posts
Showing posts with label Insert Update Delete View in recyclerview in android. Show all posts

Tuesday, 29 August 2017

RecyclerView Crud(Insert,Update,Delete,View) Example in Android

Add Library into build.gradle Using app folder
    compile 'com.android.support:recyclerview-v7:26+'
MainActivity.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"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.recyclerviewcrudexample.MainActivity">


    <TextView
        android:id="@+id/tv_name_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:text="Name : " />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_name_label"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:padding="10dp"
        android:hint="Enter Name"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/tv_email_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_below="@+id/tv_name_label"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="20dp"
        android:text="Email : " />

    <EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_name_label"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:padding="10dp"
        android:layout_below="@id/et_name"
        android:hint="Enter Email"
        android:layout_marginTop="20dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_email_label"
        android:id="@+id/layout_btn"
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_marginTop="20dp">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="false"/>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_btn"
        android:scrollbars="vertical"/>
</RelativeLayout>
item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:id="@+id/layout_item"
        android:weightSum="2.50">

        <TextView
            android:id="@+id/tv_name_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Name"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:id="@+id/tv_email_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Email"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:id="@+id/tv_delete_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:layout_gravity="center"
            android:layout_marginRight="5dp"
            android:drawableEnd="@drawable/ic_delete"
            android:layout_marginLeft="20dp"/>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>
</LinearLayout>
Create Alert Dialog User Input Using Update User When User to Click Recycler Item And Open Dialog
dialog_update.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/et_update_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:padding="15dp"
        android:hint="Enter Name"/>
    <EditText
        android:id="@+id/et_update_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:padding="15dp"
        android:hint="Enter Email"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_update_user"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:layout_margin="20dp"
            android:text="Update User"/>

        <Button
            android:id="@+id/btn_update_cancel"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:textAllCaps="false"
            android:text="Cancel"/>

    </LinearLayout>
</LinearLayout>
UserData.java
package com.example.bhaumik.recyclerviewcrudexample;

/**
 * Created by Bhaumik on 8/29/2017.
 */

public class UserData {

    String name,email;


    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
MainActivity.java
package com.example.bhaumik.recyclerviewcrudexample;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    EditText et_name,et_email,et_update_name,et_update_email;
    Button add,btn_update,btn_cancel;
    RecyclerView recyclerView;
    MyAdapter adapter;

    List<UserData> list = new ArrayList<>();

    AlertDialog.Builder builder;

    AlertDialog dialog;

    String name,email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name = (EditText) findViewById(R.id.et_name);
        et_email = (EditText) findViewById(R.id.et_email);

        add = (Button) findViewById(R.id.btn_add);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MyAdapter(list);
        recyclerView.setAdapter(adapter);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                name = et_name.getText().toString();
                email = et_email.getText().toString();

                    UserData userData = new UserData();

                    userData.setName(name);
                    userData.setEmail(email);

                   list.add(userData);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this,"User Add Success...",Toast.LENGTH_SHORT).show();

                    et_name.setText("");
                    et_email.setText("");

            }
        });
        adapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void OnItemClick(int position, UserData userData) {

                builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Update User Info");
                builder.setCancelable(false);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_update,null,false);
                InitUpdateDialog(position,view);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
            }
        });


    }

    private void InitUpdateDialog(final int position, View view) {

        et_update_name = view.findViewById(R.id.et_update_name);
        et_update_email = view.findViewById(R.id.et_update_email);
        btn_update = view.findViewById(R.id.btn_update_user);
        btn_cancel = view.findViewById(R.id.btn_update_cancel);

        et_update_name.setText(name);
        et_update_email.setText(email);

        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                name = et_update_name.getText().toString();
                email = et_update_email.getText().toString();

                UserData userData = new UserData();

                userData.setName(name);
                userData.setEmail(email);

                adapter.UpdateData(position,userData);
                Toast.makeText(MainActivity.this,"User Updated..",Toast.LENGTH_SHORT).show();

            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }
}
MyAdapter.java
package com.example.bhaumik.recyclerviewcrudexample;

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;

/**
 * Created by Bhaumik on 8/29/2017.
 */

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

    List<UserData> list = new ArrayList<>();
    ItemClickListener itemClickListener;

    public MyAdapter(List<UserData> list) {
        this.list = list;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);

        return new MyHolder(view);
    }

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

        final UserData userData = list.get(position);

        holder.tv_name.setText(userData.getName());
        holder.tv_email.setText(userData.getEmail());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.OnItemClick(position,userData);
            }
        });

        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
    }

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

    public static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_name,tv_email,tv_delete;

        public MyHolder(View itemView) {
            super(itemView);

            tv_name = itemView.findViewById(R.id.tv_name_item);
            tv_email = itemView.findViewById(R.id.tv_email_item);
            tv_delete = itemView.findViewById(R.id.tv_delete_item);
        }
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    public void UpdateData(int position,UserData userData){

        list.remove(position);
        list.add(userData);
        notifyItemChanged(position);
        notifyDataSetChanged();
    }
}
Create an Interface Using Custom Create ItemClickListener
ItemClickListener.java
package com.example.bhaumik.recyclerviewcrudexample;

/**
 * Created by Bhaumik on 8/29/2017.
 */

public interface ItemClickListener {

    void OnItemClick(int position,UserData userData);
}


Output









Ads