Wednesday, June 26, 2013

ListView with Custom Adapter

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Topics.

Populate ListView with Custom Adapter

ListView can be populated by ArrayAdapter, Database, ArrayList etc
In this post I will describe how to populate ListView using a Custom Adapter.

Have a look at my previous post
Populating ListView with Database
Populating ListView with ArrayList

ListView with Custom Adapter Example

In this example I have created a listView  and populated it with Custom Adapter.
Each of the ListView item contain two views
TextView SMS Sender : to show SMS Sender Number
TextView SMSBody : to show the SMS Body/content

Here the ListView shows the all the SMSes with Sender Number and SMSBody.

What we need to do ..
Create a Custom Adapter
and add/set  the adapter to ListView.

Add the following permission in your manifest file to read the SMS..
   <uses-permission android:name="android.permission.READ_SMS"/>
   <uses-permission android:name="android.permission.WRITE_SMS"/>


<LinearLayout xmlns:android=""


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="TextView" />

        android:text="TextView" />


public class ListViewMainActivity extends Activity
            ListView listViewSMS;
            Cursor cursor;
            SMSListAdapter smsListAdapter;
            Context context;
            protected void onCreate(Bundle savedInstanceState)

                    cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
                    // Create the Adapter
                    smsListAdapter=new SMSListAdapter(this,cursor);
                    // Set The Adapter to ListView

                    // to handle click event on listView item
                    listViewSMS.setOnItemClickListener(new OnItemClickListener()
                            public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
                                // when user clicks on ListView Item , onItemClick is called
                                // with position and View of the item which is clicked
                                // we can use the position parameter to get index of clicked item

                                TextView textViewSMSSender=(TextView)v.findViewById(;
                                TextView textViewSMSBody=(TextView)v.findViewById(;
                                String smsSender=textViewSMSSender.getText().toString();
                                String smsBody=textViewSMSBody.getText().toString();
                                // Show The Dialog with Selected SMS
                                AlertDialog dialog = new AlertDialog.Builder(context).create();
                                dialog.setTitle("SMS From : "+smsSender);
                                dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                                        new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which)
} : The Custom Adapter

public class SMSListAdapter  extends BaseAdapter
    private Context mContext;
    Cursor cursor;
    public SMSListAdapter(Context context,Cursor cur)
    public int getCount()
        // return the number of records in cursor
        return cursor.getCount();

    // getView method is called for each item of ListView
    public View getView(int position,  View view, ViewGroup parent)
                    // inflate the layout for each item of listView
                    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.listview_each_item, null);
                    // move the cursor to required position
                    // fetch the sender number and sms body from cursor
                    String senderNumber=cursor.getString(cursor.getColumnIndex("address"));
                    String smsBody=cursor.getString(cursor.getColumnIndex("body"));
                    // get the reference of textViews
                    TextView textViewConatctNumber=(TextView)view.findViewById(;
                    TextView textViewSMSBody=(TextView)view.findViewById(;
                    // Set the Sender number and smsBody to respective TextViews
                    return view;

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;

ListView with Custom Adapter


New Advance Topics:                   Android LiveWallpaer Tutorial
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swipe Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation
Android AlarmManager                 Android BootReceiver                       Vibrate Phone In a Desirable Pattern    
Developing for Different Screen Sizes           Showing Toast for Longer Time       Publishing your App
How to publish Android App on Google Play
Android TextWatcher                               Android ExpandableListView

 Beginning With Android
      Android : Introduction(What is Android)                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views

Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Advance Views
Android Spinner                                                                           Android GalleryView
Android TabWidget                                                                      Android ExpandableListView

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time

TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage

Working With Incoming Calls                                       DataBase :  Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android

Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
How to Hide Title Bar In Android
How to show an Activity in Landscape or Portrait Mode only.
How to Set an Image as Wallpaper.


  1. Thanks Kamlesh for such a easy and straight explanation.

  2. excellent. i had found so many tutorials but none was easy to understand. you made my day :D thanks

  3. Anonymous they was also a listView with custom item... but my problem is
    they was crash on "listview.setAdapter(.......) "they was return a null pointer exception... so please help me....i am also trying a solve this problem but they was not solving so if you any idea so reply me please//

  4. In my experience working directly from the cursor in the adapter makes the list really slow and uses a lot of memory and when the list is long the app crashes while scrolling

  5. Very pretty man!

    Just a question,
    "cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);"
    is a long operation, wouldn't it be better if you' do that in a new Thread or like Cursor Class defined is?

    So, as AsyncTask extension.

    Kind Regards

    1. Yes, If your table contains lot of records, then it could be a long operation, in that case you can use AsyncTask

  6. Android Custom Listview-Master world

  7. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! baseball cap companies

  8. There are all sorts of items that really are mens fashion accessories. Visit This Site Right Here

  9. We love online stores that offer free shipping. Better still if they are able to offer free shipping Crockery, Perfumes, Jewellery, Mobile Accessories

  10. Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. Final Choice fashion store

  11. ry to reduce stress filled situations. It is always easier said than done. Devote 30 minutes a day doing something you enjoy. Keep your temper under check and count to ten before losing your temper or getting aggravated. Скрабирование тела

  12. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. custom patches

  13. We do custom patches, embroidered patches, printed patches, custom Velcro patches, clothing labels, PVC patches, custom leather patches, custom keychains, and other promotional products. Following is an overview of our products. Custom Iron On Patches

  14. This is controlled by your solicitation, anyway we propose that the material you are connecting the custom iron fixes on ought to be in any event as substantial as the texture for the fix. Custom Iron On Patches

  15. I respect this article for the very much investigated substance and magnificent wording. I got so included in this material that I couldn't quit perusing. I am awed with your work and aptitude. Much obliged to you to such an extent. wholesale clothing