【Android控件】ListView
一、ListView介绍
ListView是类似于将一个界面分为一行一行,如下图:
ListView只是一个视图,而如果要在这个列表视图中显示数据,则必须要通过“Adapter”,我们通常有三种Adapter:
(1)ArrayAdapter:每行只能是一个文本;
(2)SimpleAdapter:此种方式不是很好,经过测试,只能显示一行数据;
(3)继承BaseAdapter:此种方式较灵活,可以自定义将每行对应到不同的布局xml中;
二、ListView实例
这个实例分别实现了三种ListView的使用;
工程的层次结构如下:
ListView的界面如图所示:
ArrayAdapter的显示界面如下:
SimpleAdapter的界面如下:
ExtendBaseAdapter的界面如下:
1.主界面模块:
ListViewActivity.java
- package org.xiazdong;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- public class ListViewActivity extends Activity implements OnClickListener{
- private Button btn1;
- private Button btn2;
- private Button btn3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main); //一定要先setContentView,才findViewById
- btn1 = (Button) this.findViewById(R.id.arrayAdapter);
- btn2 = (Button) this.findViewById(R.id.simpleAdapter);
- btn3 = (Button)this.findViewById(R.id.baseAdapter);
- btn1.setOnClickListener(this);
- btn2.setOnClickListener(this);
- btn3.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if(v==btn1){
- Intent intent = new Intent(ListViewActivity.this,ArrayAdapterActivity.class);
- this.startActivity(intent);
- }
- else if(v==btn2){
- Intent intent = new Intent(ListViewActivity.this,SimpleAdapterActivity.class);
- this.startActivity(intent);
- }
- else if(v==btn3){
- Intent intent = new Intent(ListViewActivity.this,ExtendBaseAdapterActivity.class);
- this.startActivity(intent);
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:text="ListView的使用"
- android:textSize="50px" />
- <Button
- android:id="@+id/arrayAdapter"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/tv"
- android:text="ArrayAdapter" >
- </Button>
- <Button
- android:id="@+id/simpleAdapter"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/arrayAdapter"
- android:text="SimpleAdapter" >
- </Button>
- <Button
- android:id="@+id/baseAdapter"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/simpleAdapter"
- android:text="ExtendBaseAdapter" >
- </Button>
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_marginBottom="142dp"
- android:layout_marginRight="16dp"
- android:text="by xiazdong"
- android:textSize="20px" />
- </RelativeLayout>
2.ArrayAdapter模块
ArrayAdapterActivity.java
- package org.xiazdong;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- /*
- * 每创建一个Activity就要在AndroidManifest.xml中声明
- * ArrayAdapter只能显示基本的字符串
- * SimpleAdapter能够显示自定义视图
- */
- public class ArrayAdapterActivity extends Activity {
- private ListView lv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- lv = new ListView(this); //ListView继承View
- List<String> list = new ArrayList<String>();
- for (int i = 0; i < 10; i++) {
- list.add("xiazdong-" + i);
- }
- ArrayAdapter<String> sa = new ArrayAdapter<String>(this, //将List赋给ArrayAdapter,采用simple_item_1样式
- android.R.layout.simple_list_item_1, list);
- lv.setAdapter(sa); //将ArrayAdapter的值映射到ListView中
- this.setContentView(lv);
- }
- }
3.SimpleAdapter模块
SimpleAdapterActivity.java
- package org.xiazdong;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class SimpleAdapterActivity extends Activity {
- private ListView lv;
- @Override
- protected void onCreate(Bundle saveBundle) {
- super.onCreate(saveBundle);
- lv = new ListView(this);
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
- Map<String, Object> map1 = new HashMap<String, Object>();
- map1.put("name1", "xiazdong-1");
- map1.put("button1", "按钮1");
- list.add(map1);
- SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.simple,
- new String[] { "name1", "button1"}, new int[] { R.id.tv1,
- R.id.btn1 });
- lv.setAdapter(sa);
- this.setContentView(lv);
- }
- }
simple.xml
- <?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="match_parent"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- <Button
- android:id="@+id/btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button" />
- </LinearLayout>
4.ExtendBaseAdapter模块
ExtendBaseAdapterActivity.java
- package org.xiazdong;
- 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 ExtendBaseAdapterActivity extends Activity {
- private ListView lv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- lv = new ListView(this);
- MyAdapter ma = new MyAdapter(this, new int[] { R.layout.sub1,
- R.layout.sub2 }); // 分别在每行中显示sub1的布局和sub2的布局
- lv.setAdapter(ma);
- this.setContentView(lv);
- }
- }
MyAdapter.java
- package org.xiazdong;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.Toast;
- public class MyAdapter extends BaseAdapter {
- private Context context;
- private int layoutID[];
- private LayoutInflater inflator;
- public MyAdapter(Context context, int layoutID[]) {
- this.context = context;
- this.layoutID = layoutID;
- inflator = LayoutInflater.from(context);
- }
- @Override
- public int getCount() { // 一个布局占一行
- return layoutID.length;
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- // position表示要显示第几行,0表示第一行
- // convertView表示要显示的视图
- public View getView(int position, View convertView, ViewGroup parent) {
- convertView = inflator.inflate(layoutID[position], null);//每次将一个布局xml映射到一项中
- if (convertView.findViewById(R.id.checkBox1) instanceof CheckBox) {
- CheckBox cb1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
- cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- Toast t = Toast.makeText(context, "点击了复选框", Toast.LENGTH_SHORT);
- t.show();
- }
- });
- }
- if (convertView.findViewById(R.id.button1) instanceof Button) {
- Button b1 = (Button) convertView.findViewById(R.id.button1);
- b1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast t = Toast.makeText(context, "点击了按钮", Toast.LENGTH_SHORT);
- t.show();
- }
- });
- }
- return convertView;
- }
- }
sub1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/checkBox1"
- android:layout_alignBottom="@+id/checkBox1"
- android:layout_alignParentLeft="true"
- android:text="点击弹出提示" />
- <CheckBox
- android:id="@+id/checkBox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:text="" />
- </RelativeLayout>
sub2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/button1"
- android:layout_alignBottom="@+id/button1"
- android:layout_alignParentLeft="true"
- android:text="TextView" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:text="Button" />
- </RelativeLayout>
总结:
ListView的应用有很多,比如
(1)在一般的游戏中列出排行榜;
(2)“设置”中列出多种子设置;
(3)字典列出单词;
(4)通讯录列表;