android中除了Activity外,还有ListActivity。
ListActivity 是布局配置应用程序 ,常用来显示菜单列表,列表的明细项目等。
ListActivity包括屏幕布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout> 行布局:<?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="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text1" android:textSize="16sp" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/text2" android:textSize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> 可以绑定数据:public class MyListAdapter extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // We'll define a custom screen layout here (the one shown above), but // typically, you could just use the standard ListActivity layout. setContentView(R.layout.custom_list_activity_view); // Query for all people contacts using theContacts.People
convenience class. // Put a managed wrapper around the retrieved cursor so we don't have to worry about // requerying or closing it as the activity changes state. mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(mCursor); // Now create a new list adapter bound to the cursor. // SimpleListAdapter is designed for binding to a Cursor. ListAdapter adapter = new SimpleCursorAdapter( this, // Context. android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). mCursor, // Pass in the cursor to bind to. new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to. new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns. // Bind to our new adapter. setListAdapter(adapter); } }