Search This Blog

Wednesday 27 June 2012

Using ListView with ArrayAdapter in Android

To understand the list view you can create a simple list view in layout file.
(locate the layout file in res directory) and the following lines in the main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 
 <TextView
        android:text="CreativeAndroidApps - This is a ListView Example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 <ListView
         android:id="@+id/list"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" />
 </LinearLayout>

Now create a array of months to populate in Listview

ListView myList;
     String[] listContent = {
                "January",
                "February",
                 "March",
                 "April",
                 "May",
                 "June",
                 "July",
                 "August",
                 "September",
                 "October",
                 "November",
                 "December"
     };

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        myList = (ListView)findViewById(R.id.list);
        //create the array adapter for the list view  
        ArrayAdapter<String> adapter
         = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1,
          listContent);
          myList.setAdapter(adapter);
     }

Now run the app and see the list view of months.

Android List
Android ListView Example








No comments:

Post a Comment