Sunday, December 23, 2012

ListView in Android (Basic)

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 and converts each item result into a view that's placed into the list.

This tutorial describes how to use ListView and ListActivity in Android.

okay! so let's try this small app

 -------------------------------------------
App Name: ListViewBasic
Package Name: com.sunil
Android SDK: Android SDK 2.3.3 / API 10
Default ListActivity Name: ActivityListView
-------------------------------------------

ActivityListView.java

    package com.sunil;   

    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 ActivityListView extends ListActivity {

     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Create an array of Strings, that will be put to our ListActivity 
     String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse", 
        "Suse", "Ubuntu", "Solaris", "Android", "iPhone" }; 
    
     /* Create an ArrayAdapter, that will actually make the Strings above
      appear in the ListView */ 

     this.setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, namesArray)); 
     } 
      @Override 
      protected void onListItemClick(ListView l, View v,  
      int position, long id) { 
     super.onListItemClick(l, v, position, id); 
      
     // Get the item that was clicked 

     Object o = this.getListAdapter().getItem(position); 
     String keyword = o.toString(); 
     Toast.makeText(this, "You selected: " + keyword,  
       Toast.LENGTH_SHORT).show(); 
     } 
    } 
 
main.xml


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

         <TextView 

          android:layout_width="fill_parent" 

          android:layout_height="wrap_content" 

          android:text="@string/hello" /> 

        </LinearLayout> 



    AndroidManifest.xml


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

          <manifest 

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

           package="com.sunil" 

           android:versionCode="1" 

           android:versionName="1.0"> 

           <uses-sdk android:minSdkVersion="10" /> 

           

           <application 

            android:icon="@drawable/icon" 

            android:label="@string/app_name"> 

            <activity 

             android:name=".ActivityListView" 

             android:label="@string/app_name"> 

             <intent-filter> 

             <action android:name="android.intent.action.MAIN" /> 

             <category android:name="android.intent.category.LAUNCHER" /> 

             </intent-filter> 

            </activity> 

           

           </application> 

          </manifest> 


         
         
      The output Screen will be like this..


      You can download the complete source code zip file here : ListViewBasic 

       cheers!!

       I'd love to hear your thoughts!

      Rocky

      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

      0 comments:

      Post a Comment

       

      Copyright @ 2013 Android Developers Tipss.