Create New project RecyclerView Custom Adapter with empty activity
Add dependencies in build.gradle as below
dependencies {
implementation 'com.android.support:recyclerview-v7:26.0.0'
}
// activity_main.xml
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout 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.krishanandroid.recyclerviewcustomadapter.MainActivity" >
<EditText android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@+id/et_eno"
android :hint= "enter eno"
android :inputType= "number"
android :textSize= "16sp"
android :gravity= "center_horizontal" />
<EditText
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@+id/et_name"
android :hint= "enter name"
android :inputType= "text"
android :textSize= "16sp"
android :gravity= "center_horizontal" />
<EditText
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@+id/et_salary"
android :hint= "enter sal"
android :inputType= "number"
android :textSize= "16sp"
android :gravity= "center_horizontal" />
<Button
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@+id/button_add"
android :text= "Add"
android :textSize= "16sp" />
<android.support.v7.widget.RecyclerView
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :id= "@+id/recyclerView1"
android :padding= "4dp" >
</android.support.v7.widget.RecyclerView >
</LinearLayout >
// MainActivity.java
package com.krishanandroid.recyclerviewcustomadapter;
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.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import static android.support.v7.widget.LinearLayoutManager.VERTICAL ;
public class MainActivity extends AppCompatActivity {
EditText mEno , mESalary , mEName ;
Button mButtonAdd ;
RecyclerView mRecyclerView ;
ArrayList<RowItems> arrayList ;
LinearLayoutManager linearLayoutManager ;
RecyclerViewAdapter myAdapter ;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.row_items , parent,false );
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override public void onBindViewHolder(ViewHolder holder, int position) {
RowItems items = arrayList .get(position);
holder.tv1 .setText("" +items.getEno());
holder.tv2 .setText(items.getEname());
holder.tv3 .setText("" +items.getEsalary());
}
@Override public int getItemCount() {
return arrayList .size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tv1 , tv2 , tv3 ;
public ViewHolder(View itemView) {
super (itemView);
tv1 = itemView.findViewById(R.id.tv1 );
tv2 = itemView.findViewById(R.id.tv2 );
tv3 = itemView.findViewById(R.id.tv3 );
}
}
}
@Override protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
mEno = findViewById(R.id.et_eno );
mESalary = findViewById(R.id.et_name );
mEName = findViewById(R.id.et_salary );
mButtonAdd = findViewById(R.id.button_add );
mRecyclerView = findViewById(R.id.recyclerView1 );
arrayList = new ArrayList<RowItems>();
myAdapter = new RecyclerViewAdapter();
linearLayoutManager = new LinearLayoutManager(MainActivity.this , VERTICAL , false );
mRecyclerView .setAdapter(myAdapter );
mRecyclerView .setLayoutManager(linearLayoutManager );
mButtonAdd .setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
String no = mEno .getText().toString();
String salary = mESalary .getText().toString();
String name = mEName .getText().toString();
RowItems rowItems = new RowItems(no, salary, name);
arrayList .add(rowItems);
myAdapter .notifyDataSetChanged();
mEno .setText(" " );
mESalary .setText(" " );
mEName .setText(" " );
mEno .requestFocus();
}
});
}
}
// row_items.xml
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent" android :layout_height= "wrap_content"
android :orientation= "horizontal" >
<TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "eno"
android :id= "@+id/tv1"
android :textSize= "16sp"
android :padding= "4dp" />
<TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "ename"
android :id= "@+id/tv2"
android :layout_marginLeft= "12dp"
android :textSize= "16sp"
android :padding= "4dp" />
<TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "esalary"
android :layout_marginLeft= "12dp"
android :id= "@+id/tv3"
android :textSize= "16sp"
android :padding= "4dp" />
</LinearLayout >
// RowItems.java
package com.krishanandroid.recyclerviewcustomadapter;
/** * Created by user on 9/13/2018. */ // Bean class public class RowItems {
String eno , ename , esalary ;
// constructors public RowItems(String eno, String ename, String esalary) {
this .eno = eno;
this .ename = ename;
this .esalary = esalary;
}
public String getEno() {
return eno ;
}
public void setEno(String eno) {
this .eno = eno;
}
public String getEname() {
return ename ;
}
public void setEname(String ename) {
this .ename = ename;
}
public String getEsalary() {
return esalary ;
}
public void setEsalary(String esalary) {
this .esalary = esalary;
}
}
Live Demo: https://www.youtube.com/watch?v=K9jVvwQ1MPs
VIDEO