ListView应用(通过Activity跳转)

通过三种适配器来展示ListView控件。

 列表的显示需要三个元素:
 1.ListVeiw 用来展示列表的View。
 2.适配器 用来把数据映射到ListView上的中介。
 3.数据 具体的将被映射的字符串,图片,或者基本组件。
 根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
 其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。
 SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

首先展示效果图:

首先是主Acitivity.

ListView
 1 package com.cb.listview;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 
 9 
10 public class Listview extends Activity {
11     private Button mArrayAdapterButton;
12     private Button mSimpleAdapterButton;
13     private Button mSimpleCursorAdapterButton;
14 
15     private String[] items = { "zero", "one", "two", "three", "four", "five",
16             "six", "seven", "eight", "nine" };
17 
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22 
23         mArrayAdapterButton = (Button) findViewById(R.id.button01);
24         mArrayAdapterButton.setOnClickListener(new OnClick());
25 
26         mSimpleAdapterButton = (Button) findViewById(R.id.button02);
27         mSimpleAdapterButton.setOnClickListener(new OnClick());
28 
29         mSimpleCursorAdapterButton = (Button) findViewById(R.id.button03);
30         mSimpleCursorAdapterButton.setOnClickListener(new OnClick());
31 
32     }
33 
34     public class OnClick implements View.OnClickListener {
35 
36         @Override
37         public void onClick(View v) {
38             GoTo();
39         }
40 
41     }
42 
43     public void GoTo() {
44         Intent intent = new Intent();
45         Bundle bundle = new Bundle();
46         bundle.putStringArray("string", items);
47         intent.putExtras(bundle);
48 
49         if (mArrayAdapterButton.isPressed()) {
50             intent.setClass(Listview.this, ArrayAdapterActivity.class);
51         } else if (mSimpleAdapterButton.isPressed()) {
52             intent.setClass(Listview.this, SimpleAdapterActivity.class);
53         } else {
54             intent.setClass(Listview.this, SimpleCursorAdapterActivity.class);
55         }
56         startActivity(intent);
57     }
58 }

这是它的main.xml文件.

main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/useListView" />
11 
12     <LinearLayout
13         android:layout_width="fill_parent"
14         android:layout_height="fill_parent"
15         android:orientation="vertical" >
16 
17         <Button
18             android:id="@+id/button01"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:text="@string/ArrayAdapter" />
22 
23         <Button
24             android:id="@+id/button02"
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="@string/SimpleAdapter" />
28 
29         <Button
30             android:id="@+id/button03"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:text="@string/SimpleCursorAdapter" />
34 
35     </LinearLayout>
36 
37 </LinearLayout>

效果图:

在ArrayAdapterActivity中,展示ArrayAdapter.

ArrayAdapter
 1 package com.cb.listview;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.widget.ArrayAdapter;
 7 import android.widget.ListView;
 8 
 9 public class ArrayAdapterActivity extends Activity {
10     private ListView mListView;
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main1);
16 
17         Intent intent = this.getIntent(); // get the intent passed by the
18                                             // Activity named Listview..
19         Bundle bundle = intent.getExtras(); // get the bundle
20 
21         String[] items = bundle.getStringArray("string"); // get the content
22                                                             // from bundle
23         /*
24          * 花了半个小时纠结"string"..(之前错写成了"String"),忏悔ing,但是为什么bundle.getStringArray()
25          * 函数不报错.. 而是new ArrayAdapter<String>()的时候报错...
26          */
27 
28         mListView = (ListView) findViewById(R.id.listview01);
29 
30         // new a object of ArrayAdapter<String>, which is constructed by items.
31         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
32                 ArrayAdapterActivity.this, android.R.layout.simple_list_item_1,
33                 items);
34 
35         mListView.setAdapter(arrayAdapter); // set the adapter of mListView
36     }
37 }

main1.xml文件.

main1.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/ArrayAdapter" />
11 
12     <ListView
13         android:id="@+id/listview01"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content" />
16 
17 </LinearLayout>

效果图:

在SimpleAdapterActivity中,展示SimpleAdapter.

SimpleAdapter
 1 package com.cb.listview;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import android.R.integer;
 9 import android.app.Activity;
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.util.Log;
13 import android.view.View;
14 import android.view.View.OnClickListener;
15 import android.widget.Button;
16 import android.widget.ListView;
17 import android.widget.SimpleAdapter;
18 import android.widget.SimpleAdapter.ViewBinder;
19 
20 public class SimpleAdapterActivity extends Activity {
21     private ListView mListView;
22     
23     //定义图片数组
24     private int[] images = new int[] { R.drawable.a, R.drawable.b,
25             R.drawable.a, R.drawable.b, R.drawable.a, R.drawable.b,
26             R.drawable.a, R.drawable.b, R.drawable.a,R.drawable.b };
27 
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.main2);
32 
33         Intent intent = this.getIntent(); // get the intent passed by the
34                                             // Activity named Listview..
35         Bundle bundle = intent.getExtras(); // get the bundle
36 
37         String[] items = bundle.getStringArray("string"); // get the content
38                                                             // from bundle
39 
40         List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
41         for (int i = 0; i < items.length; i++) {
42             Map<String, Object> listItem = new HashMap<String, Object>();
43             listItem.put("items", items[i]);
44             listItem.put("images", images[i]);
45             listItems.add(listItem);
46         }
47         /*构造函数参数解析
48          * SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
49          * context:指上下文
50          * data:参数为Map的list。list里每一项都对应ListView中的每一项;Map里的每一项都包含ListView中的每一行里的内容。
51          * resource:布局文件,可引用系统提供的,也可自定义。
52          * from:Map中的key值
53          * to:是一个整型数组,存放的是TextView控件的id
54          */
55         /*
56          * Parameters:
57          * context: The context where the View associated with this SimpleAdapter is running
58          * data: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
59          * resource: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
60          * from: A list of column names that will be added to the Map associated with each item.
61          * to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
62 
63          */
64         //用main22.xml文件来设置List中两个数据的布局
65         SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,
66                 R.layout.main22, new String[] {
67                         "items", "images" }, new int[] { R.id.textview02,
68                         R.id.imageview02 });
69         
70         mListView = (ListView) findViewById(R.id.listview02);
71         mListView.setAdapter(simpleAdapter);
72 
73     }
74 }

main2.xml文件.

main2.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <ListView
 8         android:id="@+id/listview02"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content" />
11 
12 
13 </LinearLayout>

main22.xml

main22.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="horizontal" >
 6 
 7     <ImageView
 8         android:id="@+id/imageview02"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:paddingLeft="10dp" />
12 
13     <TextView
14         android:id="@+id/textview02"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:gravity="center_vertical"
18         android:paddingLeft="10dp"
19         android:text="@string/SimpleAdapter"
20         android:textSize="16dp" />
21 
22 </LinearLayout>

效果图:

获取手机中的通讯录信息。

在SimpleCursorAdapterActivity中,展示SimpleCursorAdapter.

SimpleCursorAdapter
 1 package com.cb.listview;
 2 
 3 import android.app.Activity;
 4 import android.database.Cursor;
 5 import android.os.Bundle;
 6 import android.provider.Contacts.People;
 7 import android.widget.ListAdapter;
 8 import android.widget.ListView;
 9 import android.widget.SimpleCursorAdapter;
10 
11 public class SimpleCursorAdapterActivity extends Activity {
12     private ListView mListView;
13 
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main3);
18 
19         mListView = (ListView) findViewById(R.id.listview03);
20 
21         Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
22                 null, null, null);
23         startManagingCursor(cursor);
24 
25         /*
26          * 绑定数据
27          * 
28          * SimpleCursorAdapter允许你绑定一个游标的列到ListView上,并使用自定义的layout显示每个项目。
29          * SimpleCursorAdapter的创建 ,需要传入当前的上下文、一个layout资源,
30          * 一个游标和两个数组:一个包含使用的列的名字,另一个 (相同大小)数组包含View中的资源ID,用于显示相应列的数据值。
31          */
32         ListAdapter listAdapter = new SimpleCursorAdapter(this,
33                 android.R.layout.simple_list_item_2, cursor,
34                 new String[] { People.NAME }, new int[] { android.R.id.text1 });
35         mListView.setAdapter(listAdapter);
36 
37     }
38 }

main3.xml文件.

main3
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <ListView
 8         android:id="@+id/listview03"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content" />
11 
12     <TextView
13         android:id="@+id/textview03"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         android:text="@string/SimpleCursorAdapter" />
17 
18 </LinearLayout>

string.xml文件

string
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="hello">Hello World, Listview!</string>
 5     <string name="app_name">Listview</string>
 6     <string name="useListView">通过3种适配器,展示ListView的使用</string>
 7     <string name="ArrayAdapter">ArrayAdapter</string>
 8     <string name="SimpleAdapter">SimpleAdapter</string>
 9     <string name="SimpleCursorAdapter">SimpleCursorAdapter</string>
10 
11 </resources>

AndroidManifest.xml文件:需要加入三个Activity,这样系统才能找到你所定义的activity,然后才会跳转到对应的activity。另外,获取手机通讯里内容的话,则要添加属性<uses-permission ></uses-permission>

AndroidManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.cb.listview"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="10" />
 8 
 9     <application
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name" >
12         <activity
13             android:label="@string/app_name"
14             android:name=".Listview" >
15             <intent-filter >
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         
22         <!-- 加入以下代码,声明三个activity,以及权限 -->
23         <activity android:name=".ArrayAdapterActivity" >
24         </activity>
25         <activity android:name=".SimpleAdapterActivity" >
26         </activity>
27         <activity android:name=".SimpleCursorAdapterActivity" >
28         </activity>
29     </application>
30     <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
31 </manifest>
原文地址:https://www.cnblogs.com/chenbin7/p/2456224.html