First add
implementation 'com.android.support:recyclerview-v7:27.1.1'
to build.gradle
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/>
</LinearLayout>
contact.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/contacts" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
ContactAdapter.java
package com.example.binwin.practice;
import android.content.Context;
import android.support.annotation.NonNull;
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;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder> {
private ArrayList<Contact> mContact;
private LayoutInflater inflater;
public ContactAdapter(Context context, ArrayList<Contact> contactArrayList){
inflater=LayoutInflater.from(context);
mContact=contactArrayList;
}
@NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.contact, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Contact current = mContact.get(i);
viewHolder.nameTextView.setText(current.getPersonName());
viewHolder.contactTextView.setText(current.getMobileNumber());
}
@Override public int getItemCount() {
return mContact.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView contactTextView;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.name);
contactTextView = itemView.findViewById(R.id.contacts);
}
}
}
MainActivity.java
package com.example.binwin.practice;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView myRecyclerView = findViewById(R.id.recyclerView);
ArrayList<Contact> contactArrayList = new ArrayList<>();
Contact contact1 = new Contact();
contact1.setPersonName("Sabin Acharya");
contact1.setMobileNumber("9842358546");
contactArrayList.add(contact1);
Contact contact2 = new Contact();
contact2.setPersonName("Bikash Sundash");
contact2.setMobileNumber("9842465378");
contactArrayList.add(contact2);
Contact contact3 = new Contact();
contact3.setPersonName("Saroj Khanal");
contact3.setMobileNumber("9862198251");
contactArrayList.add(contact3);
Contact contact4 = new Contact();
contact4.setPersonName("Sangita Dahal");
contact4.setMobileNumber("9867813644");
contactArrayList.add(contact4);
ContactAdapter contactAdapter = new ContactAdapter(this, contactArrayList);
myRecyclerView.setAdapter(contactAdapter);
myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Contact.java
package com.example.binwin.practice;
public class Contact {
private String personName;
private String mobileNumber;
public void setPersonName(String personName) {
this.personName=personName;
}
public String getPersonName() {
return personName;
}
public void setMobileNumber(String mobileNumber){
this.mobileNumber=mobileNumber;
}
public String getMobileNumber() {
return mobileNumber;
}
}
No comments: