List View using Array Adapters – Part I

This entry is part 31 of 50 in the series Android Development Primer

Often times in your application when you want to list down a bunch of data you’re going to use a List View. It provides a predefined layout that helps create a list from a specific set of data. Now, inserting a list view in you activity’s layout is but trivial. Here we are going to see how to populate the list view.

It is to be remembered that a List can be implemented inside an activity in a number of ways. I will try and discuss all such ways in separate posts. Also remember that the classes we are going to use have overloaded constructors and we will be using any one of them. However, all of them are equally important in various situations, so I suggest you use the eclipse code-hinting feature to have a look at all those. Complete Source Code is at the bottom.

  • Start off an activity. Switch to the XML layout file and insert a ListView from the palette on the left.
  • Spread it all the way across the length and breadth of the activity.
  • Switch over to the MainActivity.java file and declare the ListView. 
  • Create a string array containing elements that you want in the list. Try and put enough values so that you can also experience the scrolling feature of ListView provided by default in the ListView element.
  • We are now going to populate the ListView with elements from the string array with the help of an Array Adapter.
  • In order to declare the ArrayAdapter write the below lines:
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1,values);
    
  • We can also declare the ArrayAdapter using the below declaration:
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
    
  • As I said earlier, we can use one of many constructors of the ArrayAdapter class depending on our needs and the situation
  • values is the name of the string array containing the elements to be displayed.
  • Now, we have the ArrayAdapter ready and in order to populate the list we need to assign this adapter to the ListView:
    lv.setAdapter(adapter);
    
  • lv is the name of the ListView of my activity.
  • Save your work and run it on an emulator or device.

Array_Adapter1  Array_Adapter2

Understanding the Code:

  • Adapters may be seen as the bridge between the UI components and the data source that fill the data into the UI.
  • There are various types of Adapters – ArrayAdapters, SimpleCursorAdapters, SimpleAdapters etc. We can even make custom adapters that serves our purposes.
  • The declarations that I have shown here has the below constructor:
    ArrayAdapter = new ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
    
  • Once we have setup the bridge i.e. ArrayAdapter we need to use it to populate the List. We do this by using the setAdapter() method.

COMPLETE SOURCE CODE

main_activity.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
</RelativeLayout>

MainActivity.java

package com.nero.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String values[]=new String[]{"Vergil", "Dante", "Sparda", "Nero", "Arkham", "Agni", "Rudra", "Beowulf", "Nevan"};
        ListView lv = (ListView) findViewById(R.id.list);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1,values);
        lv.setAdapter(adapter);
    }
}
Series Navigation<< Content Providers in AndroidList View using Array Adapters – Part II : Using ListActivity >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

43,751 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>