List View using Array Adapters – Part II : Using ListActivity

In the previous post, I showed you how to create a ListView and populate it with elements of a String array. If you can recall, we used a regular activity in the process. By regular activity I mean that our Java class extended Android.App.Activity class.

It is worth knowing that Android has a special class names Android.App.ListActivity that is meant specifically for a ListView purpose. In this post I am going to show you how to use ListActivity class for creating a ListView. Remember that even though it is meant specifically for a ListView, it does support other elements of an Android layout. 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. Unlike what we did in the last post, we are going to use a different declaration here.
    [java]
    ListView lv = getListView();
    [/java]
  • Obviously, since we are using a dedicated class, getListView() method makes perfect sense. Remember that this cannot be used in a regular activity.
  • Remember that a ListActivity requires your ListView to be identified as android.R.id.list. In order to do that, you need to edit your XML file to set the android:id attribute of the ListView to @+id/list. In some systems, this needs to be set to @+id/android:list.
  • 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.
  • Now declare the Array Adapter and assign it to the ListView.
  • Save your work and run it on an emulator or device.

Array_Adapter1  Array_Adapter2

COMPLETE SOURCE CODE

activity_main.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.ListActivity;
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 ListActivity {
@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 = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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 *