一、ListActivity : 如何你的Activity仅涉及到一个列表(ListVIew),那么你就该考虑使用ListActivity这个类
注意事项:1.ListActivity 里面默认包含显示的ListView视图
2.想自定义布局且显示出来,必须把ID设置为 android:id = “@id/list”
否则系统会报出这样的错误: Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘android.R.id.list‘
public class TextActivity extends ListActivity {
private String[] objs = {"北京", "上海", "天津"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(TextActivity.this,
R.layout.listview1, R.id.line3, objs);
setListAdapter(adapter);
}
}
activity_main.xml文件内容显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/line1" ---------------->致命的错误,它的ID必须为list才能编译过
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>