List View using Array Adapters – Part I

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:
    [java]
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1,values);
    [/java]
  • We can also declare the ArrayAdapter using the below declaration:
    [java]
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
    [/java]
  • 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:
    [java]
    lv.setAdapter(adapter);
    [/java]
  • 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:
    [java]
    ArrayAdapter = new ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
    [/java]
  • 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

MainActivity.java

[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);
}
}
[/java]

Leave a Comment

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