ListView是安卓开发中常用的组件之一,它的作用是在一个垂直的列表中展现出所需的项目。
接下来,我们看一下ListView的实现方法:
第一种是常见的在XML中定义然后在activity中使用findViewById来获取的方式(这个相当基础了,直接代码)
XML: <ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
Activity:
package com.example.listview1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list1);
List<String> data = new ArrayList<String>();
data.add("测试1");
data.add("测试2");
data.add("测试3");
data.add("测试4");
ArrayAdapter<String> adpter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
listView.setAdapter(adpter);
}
}
第二种是使用activity继承ListActivity来实现
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"></ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="什么都没有"></TextView>
</LinearLayout>
package com.example.listview2;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> data = new ArrayList<String>();
data.add("测试1");
data.add("测试2");
data.add("测试3");
data.add("测试4");
ArrayAdapter<String> adpter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
setListAdapter(adpter);
}
}
在第二种的Activity中,我们没有声明ListView,而是让Activy继承了ListActivity,使用setContentView绑定了ListView的样式,使用setListAdpter绑定数据。
注意,我们在第二种写法的XML中,ListView的id写成了android:id="@android:id/list" 这是一个固定的写法,这样写程序就能找到对ListView样式的定义。
我们可以看到,上面两种写法表现出来的样式是一样的。
在第二种写法中,我在配置文件中还写了下面这段配置。
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="什么都没有"></TextView>
这个TextView的id我写成了@android:id/empty 这样写的作用是,当我的ListView内容为空时,会默认显示这个TextView。
在API中是这么描述的:Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:id/empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.
我把第二个Activity中的为ArrayList添加数据的那几句代码注释掉后,运行效果如下:
以上就是ListView的基本用法。关于ListView中数据绑定的问题,将在下一篇中介绍。