To add data to RecyclerView at runtime follow the steps mentioned below:-
1) Start a new project and add support library dependency <compile 'com.android.support:recyclerview-v7'>.
2) Declare layout for your activity(MainActivity) and add the RecyclerView to this Layout.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#000">
<Button
android:id="@+id/ADDButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
3) Now initialize the adapter and widgets in MainActivity.java file.
MainActivity.java
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
private Button mAddButton;
final Context context=this;
private ArrayList<FeedItem> feedlist;
private MyAdapter myAdapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntialDeclaration();
}
public void IntialDeclaration()
{
mRecyclerView=(RecyclerView)findViewById(R.id.RecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mAddButton=(Button)findViewById(R.id.ADDButton);
mAddButton.setOnClickListener(mClickListener);
feedlist = new ArrayList<>();
}
View.OnClickListener mClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle(" Enter New Tuple");
final TextView mMailEditText, mNameEditText;
Button mADDButton, mDismiss;
mMailEditText = (EditText) dialog.findViewById(R.id.MailEditText);
mNameEditText = (EditText) dialog.findViewById(R.id.NameEditText);
mADDButton = (Button) dialog.findViewById(R.id.AddButton);
mADDButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean a=false;
boolean b=true;
boolean c=true;
String MAIL=mMailEditText.getText().toString();
String NAME=mNameEditText.getText().toString();
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if(NAME.equals(""))
{
c=false;
Toast.makeText(MainActivity.this,"please insert Name",Toast.LENGTH_LONG).show();
}
if(MAIL.equals(""))
{
b=false;
Toast.makeText(MainActivity.this,"please insert m@il id",Toast.LENGTH_LONG).show();
}
else if(MAIL.matches(EMAIL_PATTERN))
{
a=true;
}
else {
Toast.makeText(MainActivity.this,"invalid em@il id",Toast.LENGTH_LONG).show();
}
if(a&&b&&c) {
ArrayList feed = new ArrayList<FeedItem>();
FeedItem Feed = new FeedItem(NAME, MAIL);
myAdapter = new MyAdapter(MainActivity.this, feedlist);
mRecyclerView.setAdapter(myAdapter);
feedlist.add(Feed);
dialog.dismiss();
}
}
});
mDismiss = (Button) dialog.findViewById(R.id.DismissButton);
mDismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
}
4) Create a layout that will be inflated on RecyclerView .
recycle_view_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="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="#734ff3"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:id="@+id/LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/NameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="24dp"
android:text="Name"/>
<TextView
android:textColor="#000"
android:id="@+id/MailTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text="m@il Id"/>
</LinearLayout>
<ImageView
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:id="@+id/DeleteIcon"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:src="@drawable/delete"/>
<View
android:layout_alignBottom="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#fff"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"/>
</RelativeLayout>
5) I am creating another layout for dialog from where data will be inserted into RecyclerView.
dialog_layout.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/NameEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:singleLine="true"/>
<EditText
android:id="@+id/MailEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="m@il-id"
android:singleLine="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/AddButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:layout_marginLeft="50dp"
/>
<Button
android:id="@+id/DismissButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dismiss"
android:layout_marginLeft="30dp"
/>
</LinearLayout>
</LinearLayout>
6) Create a custom adapter that will be used for RecyclerView.
MyAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<FeedItem> feedItemList;
private Context mContext;
public MyAdapter(Context context,ArrayList<FeedItem>feedItemList) {
this.feedItemList=feedItemList;
this.mContext=context;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycle_view_layout, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.mMailTextView.setText(feedItemList.get(position).getMAIL());
viewHolder.mNameTextView.setText(feedItemList.get(position).getNAME());
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mNameTextView,mMailTextView;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
mNameTextView = (TextView) itemLayoutView.findViewById(R.id.NameTextView);
mMailTextView=(TextView)itemLayoutView.findViewById(R.id.MailTextView);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.DeleteIcon);
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return feedItemList.size();
}
}
7) I am using Getter and setter java file to retrieve data from dialog box and fetch data to RecyclerView custom Adapter.
Feeditem.java
public class FeedItem {
private String NAME;
private String MAIL;
FeedItem(String NAME,String MAIL)
{
this.MAIL=MAIL;
this.NAME=NAME;
}
public String getNAME()
{
return NAME;
}
public void setNAME (String NAME)
{
this.NAME=NAME;
}
public String getMAIL()
{
return MAIL;
}
public void setMAIL(String MAIL)
{
this.MAIL=MAIL;
}
}
0 Comment(s)