Learn About Custom List View Using View and Holder in Android

Introduction

 
This article explains the custom list view using view and holder in Android. Android Studio is used to create the sample.
 
In a simple ListView only one piece of data can be displayed in a row but what will happen when we need to show data in multiple columns of a ListView? For this, we need to create a Custom Adapter to show data in multiple columns of a ListView. In this ListView application, I have shown a company name and company id in two columns. For this I have crated three classes, the first is a CustomListview class where the list view is created and that stores the data into it using the CustomAdapter class. Second  I created a List model class that stores data and returns data using methods. In a Custom Adapter class, I get the arraylist by the constructor of the CustomAdapter class.
Now after taking an ArrayList i Have created a View class to get the layout and a holder that holds the views of that layout. Get the object of the arraylist into the object of the Listmodel and set the values into the textview using the setText() method.
 
Step 1
 
The following describes how to create the project.
 
Step 2
 
Create an XML file with the following in it:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <ListView  
  7.         android:id="@+id/listView"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" />  
  10.    
  11. </LinearLayout> 
Step 3
 
Create another XML file with the following in it:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="?android:attr/listPreferredItemHeight"  
  5.    >  
  6.    
  7.      <TextView  
  8.         android:id="@+id/text1"  
  9.          android:textColor="#0000ff"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="26dip"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_alignParentLeft="true"  
  14.         android:textStyle="bold"  
  15.          android:text="text1"/>  
  16.    
  17.     <TextView  
  18.         android:textColor="#0000ff"  
  19.         android:layout_marginLeft="300dp"  
  20.         android:text="text2"  
  21.         android:id="@+id/text2"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"/>  
  24. </RelativeLayout> 
Step 4
 
Create a Java class file with the following in it:
  1. package com.listactivtyapplication;  
  2.    
  3. import java.util.ArrayList;  
  4.    
  5. import android.app.Activity;  
  6. import android.app.ListActivity;  
  7. import android.content.res.Resources;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.ListView;  
  14. import android.widget.Toast;  
  15.    
  16. public class CustomListViewAndroidExample extends Activity {  
  17.    
  18.     String[] arrayString = new String[]{"IBM""TCS""MCN""WIPRO""INFOSYS""ACCENTURE"};  
  19.     CustomListViewAndroidExample customListViewAndroidExample = this;  
  20.     Listmodel list;  
  21.     ListView listView;  
  22.     ArrayList<Listmodel> arrayList = new ArrayList<Listmodel>();  
  23.     CustomAdapter adapter;  
  24.    
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.customlistviewandroidexample);  
  28.         listView = (ListView) findViewById(R.id.listView);  
  29.         setData();  
  30.         Resources resources = getResources();  
  31.         adapter = new CustomAdapter(customListViewAndroidExample, arrayList, resources);  
  32.         listView.setAdapter(adapter);  
  33.    
  34.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  35.             @Override  
  36.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
  37.    
  38.    
  39.                 Toast.makeText(getApplicationContext(), list.getCompanyname() + "\n" + list.getid(), Toast.LENGTH_LONG).show();  
  40.             }  
  41.         });  
  42.    
  43.     }  
  44.    
  45.     public void setData() {  
  46.         for (int i = 0; i <= arrayString.length - 1; i++) {  
  47.             list = new Listmodel();  
  48.             list.setCompanyName("companyname :" + arrayString[i]);  
  49.             list.setName("id :" + i);  
  50.             arrayList.add(list);  
  51.         }  
  52.     }  
Step 5
 
Create another Java class file with the following in it:
  1. package com.listactivtyapplication;  
  2. import java.util.ArrayList;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.Resources;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14. public class CustomAdapter extends BaseAdapter   
  15. {  
  16.  Activity activity;  
  17.  LayoutInflater inflater;  
  18.  ArrayList arrayList;  
  19.  Resources resource;  
  20.  Listmodel tempValues;  
  21.  CustomAdapter(Activity activty, ArrayList arrayList, Resources resource)   
  22.  {  
  23.   this.activity = activty;  
  24.   this.arrayList = arrayList;  
  25.   this.resource = resource;  
  26.   inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  27.  }  
  28.  @Override  
  29.  public int getCount()   
  30.  {  
  31.   if (arrayList.size() <= 0)  
  32.    return 1;  
  33.   return arrayList.size();  
  34.  }  
  35.  @Override  
  36.  public Object getItem(int position)   
  37.  {  
  38.   return position;  
  39.  }  
  40.  @Override  
  41.  public long getItemId(int position)   
  42.  {  
  43.   return position;  
  44.  }  
  45.  public static class ViewHolder   
  46.  {  
  47.   public TextView text;  
  48.   public TextView text1;  
  49.   public TextView text2;  
  50.  }  
  51.  public View getView(int position, View convertView, ViewGroup parent)   
  52.  {  
  53.   View vi = convertView;  
  54.   ViewHolder holder;  
  55.   if (convertView == null)   
  56.   {  
  57.    vi = inflater.inflate(R.layout.row, null);  
  58.    holder = new ViewHolder();  
  59.    holder.text1 = (TextView) vi.findViewById(R.id.text1);  
  60.    holder.text2 = (TextView) vi.findViewById(R.id.text2);  
  61.    vi.setTag(holder);  
  62.   }   
  63.   else  
  64.    holder = (ViewHolder) vi.getTag();  
  65.   if (arrayList.size() <= 0)   
  66.   {  
  67.    holder.text.setText("No Data");  
  68.   }   
  69.   else   
  70.   {  
  71.    tempValues = null;  
  72.    tempValues = (Listmodel) arrayList.get(position);  
  73.    holder.text1.setText(tempValues.getCompanyname());  
  74.    holder.text2.setText(tempValues.getid());  
  75.   }  
  76.   return vi;  
  77.  }  
Step 6
 
Create a Java class file with the following in it:
  1. package com.listactivtyapplication;  
  2.     
  3. public class Listmodel  
  4. {   
  5.     private String companyname;  
  6.     private String  id;  
  7.     private String url;  
  8.     
  9.     public void setCompanyName(String companyname) {  
  10.         this.companyname = companyname;  
  11.     }  
  12.    
  13.     public void setName(String id)  
  14.     {  
  15.         this.id=id;  
  16.     }  
  17.   public String getCompanyname()  
  18.   {  
  19.       return companyname;  
  20.   }  
  21.    
  22.     public String getid()  
  23.     {  
  24.         return id;  
  25.     }  
Step 7
 
When you run your application:
 
Clipboard007.jpg


Similar Articles