Thursday, December 27, 2012

Android beginner tutorial: Part 33 - Using ListView in ListActivity

Today we'll learn about displaying ListView content in a ListActivity.

A ListActivity is a subclass of Activity which is made for specifically displaying ListViews. It simplifies the use and implementation of ListViews.

A ListActivity expects a ListView with an id "@android:id/list" and has an optional "@android:id/empty" object which can be any View and will be displayed if the list is empty.

Let's go and create a simple layout with a single ListView in it:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    
</LinearLayout>

Now edit the MainActivity.java class. Make sure it extends ListActivity instead of Activity. Declare an array of contact names, which will be displayed.

To create an adapter for the list view, use the setListAdapter() method:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts));

Simple as that!

Full code:

package com.kircode.codeforfood_test;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity{

final String[] contacts = {"Fernando Reigle","Lorrie Chamberland","Earlene Cully","Lorrie Down",
"Serena Adolph","Sofia Tilford","Tyrone Heiner","Mathew Betterton","Fernando Padula","Hugh Gassaway",
"Jamie Tiffin","Nelson Velazco","Liza Goudy","Christian Donahoo","Ted Baisley","Jessie Holte","Christian Corrao",
"Saundra Swoboda","Margery Canez","Lance Midyett","Jamie Burlew","Allyson Trudel","Tyrone Spofford"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

But the content of a ListActivity is not limited to just a ListView. Let's recreate the application from the previous tutorial using a ListActivity. It's a simple application that has a list of contacts and a text field, that displays the currently selected contact.

Go to the layout xml and add a new TextView with id "text":

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    
</LinearLayout>

Edit the MainActivity.java class, so that it has a "text" variable, which is set to reference the "text" object in the layout in onCreate() function.

Override an onListItemClick() function and handle the event:

package com.kircode.codeforfood_test;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity{

final String[] contacts = {"Fernando Reigle","Lorrie Chamberland","Earlene Cully","Lorrie Down",
"Serena Adolph","Sofia Tilford","Tyrone Heiner","Mathew Betterton","Fernando Padula","Hugh Gassaway",
"Jamie Tiffin","Nelson Velazco","Liza Goudy","Christian Donahoo","Ted Baisley","Jessie Holte","Christian Corrao",
"Saundra Swoboda","Margery Canez","Lance Midyett","Jamie Burlew","Allyson Trudel","Tyrone Spofford"};

public TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) { 
text.setText("Selected: " + contacts[position]);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

Now you know how to add ListView objects in Activitiy and ListActivity activities. You can use whichever way you like, they both work. But I personally prefer using ListActivity when working with lists, since it was made specifically for displaying list data.

Thanks for reading!

No comments:

Post a Comment