有的时候我们需要集成ListActivity,注意点1,这个时候我们的xml中的<ListView>标签中的id属性不能够随便自己命名,而是要固定为android:id="@id/android:list",具体如下:

main3.xml:

  

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

我们在activity中想要获取这个ListView的时候,,注意点2,可以直接通过 (ListView) findViewById(android.R.id.list);的方式来获取,这也是固定的

具体如下:

  ListViewDemo.java:

 package tjuci.edu.dl;

 import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class ListViewDemo extends ListActivity {
String[] str = { "wyl", "zhangyalan", "huarong" };
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, str);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(a);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String selected = l.getItemAtPosition(position).toString();
System.out.println("checkedItem:"+selected+",position:"+position);
} }

ListActivity的注意点-LMLPHP

05-11 11:12