List View using Array Adapters – Part II : Using ListActivity

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

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.
    ListView lv = getListView();
    
  • 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

<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.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);
    }
}

Series Navigation<< List View using Array Adapters – Part IList View using Array Adapters – Part III : Using Custom Row Layouts >>

Leave a Reply

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

*

* Copy This Password *

* Type Or Paste Password Here *

42,383 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>