代码请见SimpleAdapterDemo.zip。

步骤如下:

1、创建主布局文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <ListView
  12. android:id="@+id/listview"
  13. android:layout_height="wrap_content"
  14. android:layout_width="match_parent"
  15. android:divider="#00ff00"
  16. android:dividerHeight="2dp"
  17. android:headerDividersEnabled="false"
  18. />
  19.  
  20. </RelativeLayout>

2、创建每个列表选项的视图

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/ll_listview"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal" >
  7. <ImageView
  8. android:id="@+id/iv_header"
  9. android:layout_width="0dp"
  10. android:layout_height="40dp"
  11. android:layout_weight="1"
  12. android:contentDescription="@string/header"/>
  13.  
  14. <LinearLayout
  15. android:id="@+id/ll_title_content"
  16. android:layout_width="0dp"
  17. android:layout_height="40dp"
  18. android:orientation="vertical"
  19. android:layout_weight="4">
  20.  
  21. <TextView
  22. android:id="@+id/title"
  23. android:layout_width="match_parent"
  24. android:layout_height="20dp"
  25. android:background="#000000" />
  26.  
  27. <TextView
  28. android:id="@+id/content"
  29. android:layout_width="match_parent"
  30. android:layout_height="20dp"
  31. android:background="#00ff00" />
  32. </LinearLayout>
  33.  
  34. </LinearLayout>

3、创建主类

  1. package com.ljh.listviewdemo;
  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.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.Toast;
  15. import android.app.Activity;
  16.  
  17. public class MainActivity extends Activity {
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. // (1)创建要显示的文本内容
  25. String[] arr = { "header", "title", "content" };
  26. // (2)与使用ListActivity的最大区别:使用findViewById得到一个ListView
  27. ListView lv = (ListView) findViewById(R.id.listview);
  28.  
  29. // (3)创建ArrayAdapter,其中第二个参数resource:The resource ID for a layout file
  30. // containing a TextView to use when instantiating views.是要以一个layout作为
  31. // 参数,且此layout需要包含textview。
  32. /*
  33. * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  34. * R.layout.list, arr);
  35. */
  36.  
  37. String[] titles = { "title1", "title2", "title3", "title4" };
  38. String[] contents = { "content1", "content2", "content3", "content4" };
  39. int[] headers = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
  40. R.drawable.p4 };
  41. List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
  42.  
  43. /*
  44. * Parameters:
  45. * context The context where the View associated with this
  46. * SimpleAdapter is running data A List of Maps. Each entry in the List
  47. * corresponds to one row in the list. The Maps contain the data for
  48. * each row, and should include all the entries specified in "from"
  49. * resource Resource identifier of a view layout that defines the views
  50. * for this list item. The layout file should include at least those
  51. * named views defined in "to" from A list of column names that will be
  52. * added to the Map associated with each item. to The views that should
  53. * display column in the "from" parameter. These should all be
  54. * TextViews. The first N views in this list are given the values of the
  55. * first N columns in the from parameter.
  56. */SimpleAdapter adapter = new SimpleAdapter(this, datas,
  57. R.layout.list, arr, new int[] { R.id.iv_header, R.id.title,
  58. R.id.content });
  59. for (int i = 0; i < titles.length; i++) {
  60. Map<String, Object> map = new HashMap<String, Object>();
  61. map.put("title", titles[i]);
  62. map.put("content", contents[i]);
  63. map.put("header", headers[i]);
  64. datas.add(map);
  65. }
  66. // (4)为ListActivity设置adapter.
  67. lv.setAdapter(adapter);
  68.  
  69. lv.setOnItemClickListener(new OnItemClickListener() {
  70.  
  71. // 定义当某个选项被点击时的操作。
  72. @Override
  73. public void onItemClick(AdapterView<?> parent, View view,
  74. int position, long id) {
  75. Toast.makeText(MainActivity.this,
  76. position + " item is clicked.", Toast.LENGTH_LONG)
  77. .show();
  78.  
  79. }
  80.  
  81. });
  82.  
  83. }
  84.  
  85. }

1、使用SimpleAdapter与ArrayAdapter相比,它可以支持复杂的表项结构,而不只是单一的textview。

2、其创建过程的两个主要区别是:

(1)每个列表项的布局文件不一样

(2)创建SimpleAdapter的操作更为复杂,需要构建一个List<Map<String, Object>>。

AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表的更多相关文章

  1. AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表

    见归档项目ListViewDemo.zip. 基本步骤如下: 1.创建主布局文件,里面包含一个ListView元素. <RelativeLayout xmlns:android="ht ...

  2. Android开发自学笔记(Android Studio)—4.4 AdapterView及其子类

    一.引言       AdapterView本身是一个抽象类,而它派生的子类在用法上也基本相似,只是在显示上有一定区别,因此把他们也归为一类.       AdapterView具有如下特征: Ada ...

  3. 第3组UI组件:AdapterView及其子类

    1 AdapterView类简介 1.1 AdapterView组件是一组重要的组件,AdapterView本身是一个抽线类,实际使用更多的都是Adapter相关子类,AdapterView具有如下特 ...

  4. android-UI组件(四):AdapterView及其子类

    http://blog.csdn.net/litianpenghaha/article/details/23270881 AdapterView组件是一组重要的组件,AdapterView本身是一个抽 ...

  5. UI组件之AdapterView及其子类关系,Adapter接口及事实上现类关系

    AdapterView本身是一个抽象基类,它派生的的子类在使用方法上十分类似.AdapterView直接派生的三个子类:AbsListView.AbsSpinner,AdapterViewAnimat ...

  6. Android学习笔记(23):列表项的容器—AdapterView的子类们

    AdapterView的子类的子类ListView.GridView.Spinner.Gallery.AdapterViewFlipper和StackView都是作为容器使用,Adapter负责提供各 ...

  7. 滚动视图、列表视图[ListView、SimpleAdapter类]

    滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_ ...

  8. 第四组UI组件:AdapterView及子类

    AdapterView组件是一组重要的组件,AdapterView本省是一个抽象基类,它派生的子类在用法上十分相似,只是显示界面与一定的区别,因此这次针对它们的共性集中讲解,并突出介绍他们的区别. A ...

  9. Android的AdapterView及其子类简介-android学习之旅(二十三)

    AdapterView简介 AdapterView组件是一类非常重要的组件,AdapterView本身是一根抽象基类,继承于ViewGroup,用法十分相似,只是显示形式不一样,因此同意讲解. Ada ...

随机推荐

  1. [原创]Windows下更改特定后缀名以及特定URL前缀的默认打开方式

    Windows下,特定后缀名的文件会由特定的应用程序来运行,比如双击readme.txt,通常情况下会由Windows自带的notepad.exe(记事本)打开文件.如果现在安装了记事本以外的其他文本 ...

  2. TextView之一:子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...

  3. jQuery模糊选择

    属性字头选择器(Attribute Contains Prefix Selector) jQuery 属性字头选择器的使用格式是 jQuery(‘[attribute|=value]‘) ,例如 jQ ...

  4. label的for属性与inputde的id元素绑定

    <form> <label for="male">Male</label> <input type="radio" n ...

  5. ZendFramework 环境部署

    查看源码 int_autoloader.php 文件中,发现应用了一个 AutoloaderFactory 的命名空间,路径写得是相对路径,所以需要在 php.ini 中定义一个 inclde_pat ...

  6. What is an http upgrade?

    HTTP Upgrade is used to indicate a preference or requirement to switch to a different version of HTT ...

  7. Ubuntu14.0.4 64位安装ADT问题

    将ADT 解压之后,新建Android工程后没有R文件: google之后说要安装 ia32-libs 提示如下: 安装lib32z1 安装完成后,再次新建工程,报错如下: 编译存在问题:则继续安装以 ...

  8. Socket也有专门的Unicode版本

    https://www.chilkatsoft.com/refdoc/wcppCkSocketWRef.html https://www.chilkatsoft.com/refdoc/vcCkSock ...

  9. java设计模式--创建模式--工厂方法

    工厂方法定义: 工厂方法 概述 定义一个用于创建对象的接口,让子类决定实例化哪一个类.FactoryMethod使一个类的实例化延迟到其子类. 适用性 .当一个类不知道它所必须创建的对象的类的时候. ...

  10. UVA_Cubic Eight-Puzzle UVA 1604

    Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.Faces of cube ...