spuking4
Memory Management Pro
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:
And also open and edit your main.xml file that can be found under res/layout with the following code:
Run and test your ListView. You will see the same result just like the image above.
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:
- package
com.sourcecodester.listview
;
- import
android.app.ListActivity
;
- import
android.os.Bundle
;
- import
android.view.View
;
- import
android.widget.ArrayAdapter
;
- import
android.widget.Toast
;
- public
class
ListView
extends
ListActivity {
- ListView
listview;
- @Override
- public
void
onCreate(
Bundle savedInstanceState)
{
- super
.onCreate
(
savedInstanceState)
;
- setContentView(
R.layout
.main
)
;
- String
[
]
values =
new
String
[
]
{
"Visual Basic"
, "C#"
, "C/C++"
,
- "PHP"
, "Foxpro"
, "Delphi"
, "Java"
, "Perl"
,
- "Ruby"
, "Cobol"
}
;
- ArrayAdapter<
String>
adapter =
new
ArrayAdapter<
String>
(
this
,
- android.R
.layout
.simple_list_item_1
, values)
;
- setListAdapter(
adapter)
;
- }
- protected
void
onListItemClick(
ListView
l, View
v, int
position, long
id)
{
- String
item =
(
String
)
getListAdapter(
)
.getItem
(
position)
;
- Toast.makeText
(
this
, item +
" selected"
, Toast.LENGTH_LONG
)
.show
(
)
;
- }
- }
And also open and edit your main.xml file that can be found under res/layout with the following code:
- <?
xml version=
"1.0"
encoding=
"utf-8"
?>
- <
LinearLayout xmlns:
android=
"http://schemas.android.com/apk/res/android"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"match_parent"
- android:
orientation=
"vertical"
>
- <
ListView
- android:
id=
"@android:id/list"
- android:
layout_width=
"wrap_content"
- android:
layout_height=
"435dp"
/>
- </
LinearLayout>
Run and test your ListView. You will see the same result just like the image above.