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(); } } |
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 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 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
0 comments:
Post a Comment