Custom ListView in Android
I have already discussed how to create ListView in Android . If you have not visit the page then click here . Custom ListView is used to fulfill your own requirement . As a example , you want to show each employee details of their name,id,age,dob and gender then you have to create a custom ListView. Lets see how to create a custom ListView in Android .
Step 1: Create Project
a) Open Android Studio
b) Go to File >New> New Project > Project Name > Next > Next > Next > Finish
Step 2 : Layout Design
open activity_main.xml file and add the bellow code
<?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:background="#ff33b5e5">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Step 3 : Layout file for each row of ListView
create a file adapter_list and add the bellow code
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvDob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
<TextView
android:id="@+id/tvGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="#000000"
android:text=""/>
</LinearLayout>
</RelativeLayout>
Step 4: Create a Model class (ModelList)to store each employee details
/**
* Created by anupam on 4/10/16.
*/
public class ModelList {
String name,id,age,dob,sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Step 5 : Code
open MainActivity class and add the bellow code
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import app.demo.utils.ModelList;
/**
* Created by anupam on 04/10/16.
*/
public class MainActivity extends Activity {
Context context;
ListView listView;
ArrayList<ModelList> listOfContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize context
context = this;
listOfContact = new ArrayList<ModelList>();
// initialize listview
listView = (ListView) findViewById(R.id.list);
// adding data to array list
for (int i = 0; i < 5; i++) {
ModelList list = new ModelList();
if (i == 0) {
list.setName("Anupam ");
list.setSex("Male");
} else if (i == 1) {
list.setName("Koushik");
list.setSex("Male");
} else if (i == 2) {
list.setName("Souvik ");
list.setSex("Male");
} else if (i == 3) {
list.setName("Soumita ");
list.setSex("Female");
} else {
list.setName("Santu");
list.setSex("Male");
}
list.setAge("28");
list.setDob("01/01/1988");
list.setId("" + (i + 1));
listOfContact.add(list);
}
// calling Adapter
AdapterList adapter = new AdapterList(context, listOfContact);
// set adapter to listview
listView.setAdapter(adapter);
}
}
Step 6 : Create a Adapter class (AdapterList)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import app.demo.utils.ModelList;
/**
* Created by anupam on 4/10/16.
*/
public class AdapterList extends BaseAdapter {
Context context;
ArrayList<ModelList> _list;
LayoutInflater inflater = null;
public AdapterList(Context context, ArrayList<ModelList> _list) {
this.context = context;
this._list = _list;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return _list.size();
}
@Override
public Object getItem(int position) {
return _list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
rowView = inflater.inflate(R.layout.adapter_list, null);
TextView tvId = (TextView) rowView.findViewById(R.id.tvId);
TextView tvName = (TextView) rowView.findViewById(R.id.tvName);
TextView tvDob = (TextView) rowView.findViewById(R.id.tvDob);
TextView tvAge = (TextView) rowView.findViewById(R.id.tvAge);
TextView tvGender = (TextView) rowView.findViewById(R.id.tvGender);
ModelList list = _list.get(position);
tvId.setText("Employee Id :: " + list.getId());
tvName.setText("Employee Name :: " + list.getName());
tvDob.setText("Employee Date of Birth :: " + list.getDob());
tvAge.setText("Employee Age :: " + list.getAge());
tvGender.setText("Employee Gender :: " + list.getSex());
return rowView;
}
}
Step 7 : Output
Find Us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment