Android: ListView in ListActivity

3 September 2012 By Nithya Vasudevan 15,737 views One Comment
7 Flares Twitter 0 Facebook 0 Google+ 7 7 Flares ×

Project Description

  • This Android tutorial shows how to use ListView in ListActivity. ListActivity has a default layout that consists of a single ListView object. If the layout contains only ListView, then it is not required to assign a layout to ListActivity via the setContentView() method.
  • Here, we define string array programatically and use ArrayAdapter to bind the data items.
  • Here, we are going to implement OnItemClickListener event listener which calls onItemClick() callback method where we retrieve and display the item selected by the user.

Environment Used

  • JDK 6 (Java SE 6)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • Android SDK 4.0.3 / 4.1 Jelly Bean
  • Android Development Tools (ADT) Plugin for Eclipse (ADT version 20.0.0)
  • Refer this link to setup the Android development environment

Prerequisites

Create Android Project

  • Create a new Android Project and name it as “ListViewDemo“.
  • Enter the package name as “com.theopentutorials.android“.
  • Enter the ListActivity name as “ListViewInListActivity“.
  • Click Finish.

strings.xml

Open res/values/string.xml and replace it with following content.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ListViewInActivity!</string>
    <string name="app_name">ListViewDemo</string>
</resources>

XML layout file

Create a new XML file named “list_item.xml” in res/layout folder to define a layout for each row in the ListView. Copy the following code.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.theopentutorials.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ListViewInListActivity"
            android:label="@string/app_name" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        
    </application>
</manifest>

Activity class

Make the class extend ListActivity instead of Activity.

package com.theopentutorials.android;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewInListActivity extends ListActivity 
		implements OnItemClickListener{
    ListView listView;
    static final String[] SPORTS = {"Shuttle Badminton", "Tennis", "FootBall",
				"Basket Ball","Table Tennis", "Chess","Hockey"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        listView = getListView();
              
        //Own row layout
        listView.setAdapter(new ArrayAdapter<String>(this, 
					R.layout.list_item, SPORTS));        
        listView.setOnItemClickListener(this);  
    }
    
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, 
  				int position, long id) {
	Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	  	          Toast.LENGTH_SHORT).show();
    }
}
  • We have used ListView inside ListActivity. To retrieve the ListView object we call the method getListView().
  • For simplicity, we hard-coded a string array instead of using string array defined in external resource (res/values/strings.xml) and used it in ArrayAdapter to load the entries.

Output

Run your application

Project Folder Structure

The complete folder structure of this example is shown below.

Tags: , , , , , , , , , ,

  • Omi

    hello,
    I am working in android. I took reference of your example in my example where i populate value from json.I want to display selected value from one activity to another activity i did same which you did. but in my case not selected key go to next activity rather its value going to next activity.