Simple ListView in Android – How to Make ListView in Android

Here, In these blog we can make a one list type view which is known as ListView in Android and fill that view with some static data. these is simplest way of making list view in your android application and also, we will learn how to make it step by step with writing program.

Followed below steps :

(1) Make Design :

Here, our output is looks like below fig. which shows names of planet in ListView for Android Application.

Android ListView
Android ListView

Our ListView is defined in the main layout file (res/layout/main.xml) within a LinearLayout. Here, write below code for the design.

<?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 );

(2) Write Code in RowLayout File :

Each row in the ListView will be a TextView. The TextView is defined in another file (res/layout/simplerow.xml). Here each row of listview only show text of name of planet so, we put TextView. For that write below code in simplerow.xml file :

<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>

 (3) Activity file :

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.

For these, make a code with given below and make static list of planet. So, follow below code :

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

Finally, Our list display in Android Device screen.  Yuppiee . . .

i Hart
i Hart


This post has been seen 914 times.

Team Maffat

News Maffat Com is a Free Guest Blogging Website. Must Try once.