Sometimes we need to integrate listactivity, Note point 1, this time in our XML <ListView> tags in the id attribute can not be arbitrarily named, but to be fixed to android:id= "@id/ Android:list ", specifically as follows:
Main3.xml:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7 <ListView8 Android:id= "@id/android:list"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" /> One A </LinearLayout>
When we want to get this ListView in activity, note that point 2 can be directly via (ListView) Findviewbyid (Android. R.id.list); the way to get, which is also fixed .
Specific as follows:
Listviewdemo.java:
1 Packagetjuci.edu.dl;2 3 Importandroid.app.Activity;4 Importandroid.app.ListActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.ArrayAdapter;8 ImportAndroid.widget.ListView;9 Ten Public classListviewdemoextendslistactivity { Onestring[] str = {"Wyl", "Zhangyalan", "Huarong" }; A ListView LV; - @Override - protected voidonCreate (Bundle savedinstancestate) { the //TODO auto-generated Method Stub - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main3); -Arrayadapter<string> A =NewArrayadapter<string> ( This, + Android. R.layout.simple_list_item_1, str); -LV =(ListView) Findviewbyid (Android. r.id.list); + Lv.setadapter (a); A } at @Override - protected voidOnlistitemclick (ListView L, View V,intPositionLongID) { - //TODO auto-generated Method Stub - Super. Onlistitemclick (l, V, position, id); -String selected =l.getitematposition (position). toString (); -System.out.println ("Checkeditem:" +selected+ ", Position:" +position); in } - to +}
Listactivity Point of attention