Android Programming – Android List View Questions and Answers

Q-1 What is Listview. Write a java class to add any 10 items within Listview.

 

ListView is a view group that displays a list of scrollable items.

The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.

 

Example to list Planet names in List View:

Basically we create a ListView class and use TextView objects for each row. Each planet name is rendered in a TextView

 

Main Layout File

Our ListView is defined in the main layout file (res/layout/main.xml) within a LinearLayout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ListView android:layout_width="fill_parent"android:layout_height="fill_parent"

android:id="@+id/mainListView">

</ListView>

</LinearLayout>

 

 

The resource ID of the ListView is mainListView, which we will use to get a reference to the ListView in our Activity class.

// Find the ListView resource.

mainListView = (ListView) findViewById( R.id.mainListView );

 

 

Row Layout File

Each row in the ListView will be a TextView. The TextView is defined in another file (res/layout/simplerow.xml).

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/rowTextView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:textSize="16sp" >

</TextView>

 

 

Activity

Our main activity (SimpleListViewActivity) creates an ArrayAdapter, which holds the objects to be displayed in the ListView.

The ArrayAdapter constructor is passed the resource ID of the TextView layout file (R.layout.simplerow)

The ArrayAdapter will use it to instantiate a TextView for each row.

We then set the ArrayAdapter as our ListView’s adapter.

package com.example.android;

import java.util.ArrayList;

import java.util.Arrays;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class SimpleListViewActivity extends Activity {

private ListView mainListView ;

private ArrayAdapter<String> listAdapter ;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

// Find the ListView resource.

mainListView = (ListView) findViewById( R.id.mainListView );

// Create and populate a List of planet names.

String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",

"Jupiter", "Saturn", "Uranus", "Neptune"};

ArrayList<String> planetList = new ArrayList<String>();

planetList.addAll( Arrays.asList(planets) );

// Create ArrayAdapter using the planet list.

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,

planetList);

// Add more planets. If you passed a String[] instead of a List<String>

// into the ArrayAdapter constructor, you must not add more items.

// Otherwise an exception will occur.

listAdapter.add( "Ceres" );

listAdapter.add( "Pluto" );

listAdapter.add( "Haumea" );

listAdapter.add( "Makemake" );

listAdapter.add( "Eris" );

// Set the ArrayAdapter as the ListView's adapter.

mainListView.setAdapter( listAdapter );

}

}