How do I create a List in Android?

1. Following are the key steps in creating a Simple List.

A.  Extend ListActivity. This provides capability to set content of the list from a datasource, such as a String Array.

                         public class ListSimpleDemoActivity extends ListActivity {

                         B. Create an ArrayAdapter using a String Array

                         String[] elements = getElements();

                         //Create an ArrayAdapter

                         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,

                       android.R.layout.simple_list_item_1,

                       elements);

C. Set ListAdapter by providing your ArrayAdapter

                         this.setListAdapter(arrayAdapter);

D. onListItemClick responds to when you click on an item. It can be used to perform any action. E.g., In this example we will create a toast that shows the current time and a message.

                         protected void onListItemClick(ListView l, View v, int position, long id) {

2. Your completed Activity class should look like the following:

 

package com.sansatechnology.listsimpledemo;

 

import java.util.Date;

 

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

 

public class ListSimpleDemoActivity extends ListActivity {

 

           

            /**

             * This method is executed when the activity is created

             *

             * @see android.app.Activity#onCreate(android.os.Bundle)

             */

            @Override        

            public void onCreate(Bundle savedInstanceState) {

                  super.onCreate(savedInstanceState);

 

                  // Get the elements for the list

                  String[] elements = getElements();

                 

                  //Create an ArrayAdapter

                  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,

                              android.R.layout.simple_list_item_1,

                              elements);

                 

                  //Set the elements using List Adapter

                  this.setListAdapter(arrayAdapter);

                 

                             

            }

           

            /**

             * This method will be triggered when you pick up an item from the list

             *

             */

            @Override

            protected void onListItemClick(ListView l, View v, int position, long id) {

                  super.onListItemClick(l, v, position, id);

                   

                 

                  Object o = this.getListAdapter().getItem(position);

                  String element = o.toString();

                 

                  Toast.makeText(this, “Time now :”

                              + new Date()

                              + “\n I like ”

                              + element, Toast.LENGTH_LONG)

                              .show();

                 

                 

            }

 

           

            public String[] getElements() {

                  return new String[] { “Pizza”, “Burger”, “Burrito”, “Biryani” };

            }

 

      }

2. Go to your project and Right click to run as an Android Project.

3. You should see your application working as:

Sansa Technology Android Training simple list example image 1

Sansa Technology Android Training simple list example image 2

About sansatechnology

Sansa Technology is an IT staffing, Consulting and Training company located in the heart of Silicon Valley. If you are a consultant looking for your new gig, send us your resume to us. If you are looking for training in the field of White Box Testing, Java, Java EE, Selenium, DevOps, Android and others, connect with us as well. Visit us at www.sansatechnology.com or email contact@sansatechnology.com
This entry was posted in Computers and Internet and tagged . Bookmark the permalink.

3 Responses to How do I create a List in Android?

  1. Sampreeth says:

    very helpful post. Thanks

  2. Well, I am so excited that I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject.
    Play android games

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s