Objective- : In this blog, we are going to make employee list with Profile Picture, Name and Designation Like
For making these kind of List we need to customized Listview component of ListView
Step #1-: Create New Project CustomListView
Goto File > New > Android Application Project. Complete the create new project wizard.
Step #2-: Creating Row layout : as per requirement.
Goto res > right click > New > Choose Android XML File > name custom_row.xml > Finish.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/employee_picture"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/employee_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/employee_name"
android:layout_toRightOf="@+id/employee_picture"
android:layout_marginTop="15dp"
/>
<TextView
android:id="@+id/employee_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/employee_designation"
android:layout_alignLeft="@+id/employee_name"
android:layout_below="@+id/employee_name"
/>
</RelativeLayout>
Step #3-: Create custom_listvie.xml
<RelativeLayout 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"
>
<ListView
android:id="@+id/employeeList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Step #4-: Creater EmployeeDataClass : For storing data of Employees
package com.example.cutomlistview;
public class EmployeeDataClass {
public int employeePictureId;
public String employeeName;
public String employeeDesignation;
public void setEmployeePictureId(int employeePictureId) {
this.employeePictureId = employeePictureId;
}
public int getEmployeePictureId() {
return employeePictureId;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public void setEmployeeDesignation(String employeeDesignation) {
this.employeeDesignation = employeeDesignation;
}
public String getEmployeeName() {
return employeeName;
}
public String getEmployeeDesignation() {
return employeeDesignation;
}
}
Step #5-: Create CustomBaseAdapter : It is a combination of data and view
package com.example.cutomlistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomBaseAdapter extends BaseAdapter{
ArrayList<EmployeeDataClass> mEmployeeList;
Context mContext;
LayoutInflater mInflater;
ViewHolder mViewHolder;
public CustomBaseAdapter(Context context, ArrayList<EmployeeDataClass> employeeList){
this.mContext=context;
this.mEmployeeList=employeeList;
this.mInflater=LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return this.mEmployeeList.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int index, View customView, ViewGroup arg2) {
if(customView==null||customView.getTag()==null){
customView=mInflater.inflate(R.layout.custom_row, null);
mViewHolder=new ViewHolder();
mViewHolder.employeeProfilePic=(ImageView)customView.findViewById(R.id.employee_picture);
mViewHolder.employeeName=(TextView)customView.findViewById(R.id.employee_name);
mViewHolder.employeeDesignation=(TextView)customView.findViewById(R.id.employee_designation);
customView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolder)customView.getTag();
}
mViewHolder.employeeProfilePic.setImageResource(mEmployeeList.get(index).getEmployeePictureId());
mViewHolder.employeeName.setText(mEmployeeList.get(index).getEmployeeName().toString());
mViewHolder.employeeDesignation.setText(mEmployeeList.get(index).getEmployeeDesignation().toString());
return customView;
}
private static class ViewHolder{
ImageView employeeProfilePic;
TextView employeeName;
TextView employeeDesignation;
}}
Step #6-: Creating ItemClickListner : List view Item Click Listener
package com.example.cutomlistview;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class ItemClickListner implements OnItemClickListener{
Context mContext;
public ItemClickListner(Context context){
mContext=context;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(mContext,"Employee Position =>"+position, Toast.LENGTH_LONG).show();
}
}
Step #7-: Creating CustomListViewActivity : For display View on Android device.
package com.example.cutomlistview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class CustomListViewActivity extends Activity{
ListView mEmployeeList;
EmployeeDataClass mEmployee;
CustomBaseAdapter mCustomBaseAdapter;
ItemClickListner mItemClickListner;
ArrayList<EmployeeDataClass> mEmployeeListData=new ArrayList<EmployeeDataClass>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview);
initilization();
}
public void initilization(){
mEmployeeList=(ListView)this.findViewById(R.id.employeeList);
settingEmployeeData();
mCustomBaseAdapter=new CustomBaseAdapter(this,mEmployeeListData);
mEmployeeList.setAdapter(mCustomBaseAdapter);
mItemClickListner=new ItemClickListner(this);
mEmployeeList.setOnItemClickListener(mItemClickListner);
}
public void settingEmployeeData(){
mEmployee=new EmployeeDataClass();
mEmployee.setEmployeePictureId(R.drawable.harikrishana_profilepic);
mEmployee.setEmployeeName("Hari Krishana");
mEmployee.setEmployeeDesignation("Software Engineer");
mEmployeeListData.add(mEmployee);
mEmployee=new EmployeeDataClass();
mEmployee.setEmployeePictureId(R.drawable.ravishankar_profilepic);
mEmployee.setEmployeeName("Ravi Shankar");
mEmployee.setEmployeeDesignation("Software Engineer");
mEmployeeListData.add(mEmployee);
}
}
Step #8 : Running CustomListView Application : Run > Run
OUTPUT :
You can download the whole source code of this project CustomListView.zip


No comments:
Post a Comment