Simple listView using ArrayAdapter

Guest Posts coolbhushans

In this tutorial I am creating simple list view for Android.

The display of elements in a lists is a very common pattern in mobile applications. The user sees a list of items and can scroll through them. Such an activity is depicted in the following picture.

ART4_image1

 

List in Android :

Select the Composite Component  from  Palette and set the ListView to activitymain.xml file.

Android provides the ListView or ExpandableListView classes which is capable of displaying a scrollable list of items. ExpandableListView supports also a grouping of items. The items in the list can be of image list,text list,or any other type.

ArrayAdapter:

In simple terms, an Adaptor is a collection handler that returns each item in the collection as a view. ArrayAdaptor is one of many adaptors available in Android

The ArrayAdapter class can handle a list or array of Java objects as input. Every Java object is mapped to one row. By default it maps the toString() method of the object to a view in the row layout.

An adapter is used for managing the items in the list (the data model or data source).

The ArrayAdapter is limited as it supports only the mapping of the toString() to one view in the row layout.

So we want to create custom Adapter.

<!--[if gte mso 9]>-->

res/layout/activitymain.xml


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

xmlns:tools="http://schemas.android.com/tools"

android:layoutwidth="matchparent"

android:layoutheight="matchparent"

android:paddingBottom="@dimen/activityverticalmargin"

android:paddingLeft="@dimen/activityhorizontalmargin"

android:paddingRight="@dimen/activityhorizontalmargin"

android:paddingTop="@dimen/activityverticalmargin"

tools:context=".MainActivity" >

<ListView android:layoutwidth="fillparent"

android:layoutheight="fillparent"

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

</ListView>

</LinearLayout>

  
 
src/MainActivity.java

 

 package com.gnetspace.simplelistview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
    ListView lv;
       String[] planets = new String[] { "Apple", "Banana", "Mango", "Grapes"  
    };    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView);
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, planets);

              // Assign adapter to ListView
              lv.setAdapter(adapter);

              lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
                     int itemPosition     = position;

                       // ListView Clicked item value
                       String  itemValue    = (String) lv.getItemAtPosition(position);

                        // Show Alert 
                        Toast.makeText(getApplicationContext(),
                          "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                          .show();
                }
            });
    }

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

}
 

ART4_image2