• We just launched and are currently in beta. Join us as we build and grow the community.

Android List View Example

spuking4

Memory Management Pro
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
171
Likes
185
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
A ListView in Android is a scrollable item that is displayed using an Adapter from an array or database. An example of this is the messaging system of a mobile phone.

In this tutorial, I will show you the basic of ListView. To avoid complex code, I will simply use an array instead of database to display the item in our ListView.

Now, create an Android project. If you don’t know how to create one, please follow our tutorial on how to “Create an Android Project Using Eclipse“.

Settings:

Application Name: List View

Project Name: listview

Package Name: com.sourcecodester.listview

Activity Name: ListView

Layout Name: main

If you have followed our previous tutorial, you will probably know the settings above.

Now, open and edit your ListView.java with the following code:

  1. package

    com.sourcecodester.listview

    ;

  2. import

    android.app.ListActivity

    ;
  3. import

    android.os.Bundle

    ;
  4. import

    android.view.View

    ;
  5. import

    android.widget.ArrayAdapter

    ;
  6. import

    android.widget.Toast

    ;

  7. public

    class

    ListView

    extends

    ListActivity {
  8. ListView

    listview;

  9. @Override
  10. public

    void

    onCreate(

    Bundle savedInstanceState)

    {
  11. super

    .onCreate

    (

    savedInstanceState)

    ;
  12. setContentView(

    R.layout

    .main

    )

    ;

  13. String

    [

    ]

    values =

    new

    String

    [

    ]

    {

    "Visual Basic"

    , "C#"

    , "C/C++"

    ,
  14. "PHP"

    , "Foxpro"

    , "Delphi"

    , "Java"

    , "Perl"

    ,
  15. "Ruby"

    , "Cobol"

    }

    ;

  16. ArrayAdapter<

    String>

    adapter =

    new

    ArrayAdapter<

    String>

    (

    this

    ,
  17. android.R

    .layout

    .simple_list_item_1

    , values)

    ;

  18. setListAdapter(

    adapter)

    ;
  19. }

  20. protected

    void

    onListItemClick(

    ListView

    l, View

    v, int

    position, long

    id)

    {
  21. String

    item =

    (

    String

    )

    getListAdapter(

    )

    .getItem

    (

    position)

    ;
  22. Toast.makeText

    (

    this

    , item +

    " selected"

    , Toast.LENGTH_LONG

    )

    .show

    (

    )

    ;
  23. }

  24. }

And also open and edit your main.xml file that can be found under res/layout with the following code:

  1. <?

    xml version=

    "1.0"

    encoding=

    "utf-8"

    ?>
  2. <

    LinearLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. android:

    layout_width=

    "match_parent"
  4. android:

    layout_height=

    "match_parent"
  5. android:

    orientation=

    "vertical"

    >

  6. <

    ListView


  7. android:

    id=

    "@android:id/list"
  8. android:

    layout_width=

    "wrap_content"
  9. android:

    layout_height=

    "435dp"

    />

  10. </

    LinearLayout>

Run and test your ListView. You will see the same result just like the image above.

 

440,010

316,559

316,568

Top