Welcome to ANDROID Developer!

Friday, May 23, 2014

ListView in Android (Basic)


By on 3:35 AM


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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    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

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
        <?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

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
          <?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 

      About Theavuth NHEL

      Faizan is a 17 year old young guy who is blessed with the art of Blogging,He love to Blog day in and day out,He is a Website Designer and a Certified Graphics Designer.

      0 comments:

      Post a Comment