代码请见SimpleAdapterDemo.zip。

步骤如下:

1、创建主布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="@+id/listview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#00ff00"
android:dividerHeight="2dp"
android:headerDividersEnabled="false"
/> </RelativeLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_header"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:contentDescription="@string/header"/> <LinearLayout
android:id="@+id/ll_title_content"
android:layout_width="0dp"
android:layout_height="40dp"
android:orientation="vertical"
android:layout_weight="4"> <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#000000" /> <TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#00ff00" />
</LinearLayout> </LinearLayout>

3、创建主类

package com.ljh.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // (1)创建要显示的文本内容
String[] arr = { "header", "title", "content" };
// (2)与使用ListActivity的最大区别:使用findViewById得到一个ListView
ListView lv = (ListView) findViewById(R.id.listview); // (3)创建ArrayAdapter,其中第二个参数resource:The resource ID for a layout file
// containing a TextView to use when instantiating views.是要以一个layout作为
// 参数,且此layout需要包含textview。
/*
* ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
* R.layout.list, arr);
*/ String[] titles = { "title1", "title2", "title3", "title4" };
String[] contents = { "content1", "content2", "content3", "content4" };
int[] headers = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4 };
List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>(); /*
* Parameters:
* context The context where the View associated with this
* SimpleAdapter is running 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"
* 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" from A list of column names that will be
* added to the Map associated with each item. 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.
*/SimpleAdapter adapter = new SimpleAdapter(this, datas,
R.layout.list, arr, new int[] { R.id.iv_header, R.id.title,
R.id.content });
for (int i = 0; i < titles.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", titles[i]);
map.put("content", contents[i]);
map.put("header", headers[i]);
datas.add(map);
}
// (4)为ListActivity设置adapter.
lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { // 定义当某个选项被点击时的操作。
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
position + " item is clicked.", Toast.LENGTH_LONG)
.show(); } }); } }

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. Linux中Firefox——Httpfox插件安装及使用

    Httpfox插件安装步骤: 1.打开firefox浏览器,点击左上方"工具"中的"附加组件" 2.在弹出页中搜索"Httpfox",点击下 ...

  2. VS2010无法断点调试解决办法

    今天我的VS2010忽然出现设置断点的时候,无法进行调试.现象:每次在设置断点调试的时候都会出现卡机,然后VS弹出如下图所示的状况: 解决办法: 依次点击:“工具-->扩展管理器”然后找到.Ne ...

  3. 10 个十分难得的 javascript 开发经验

    Javascript 的很多扩展的特性是的它变得更加的犀利, 同时也给予程序员机会创建更漂亮并且更让用户喜欢的网站. 尽管很多的开发人员都乐于颂扬 javascript,但是仍旧有人看到它的阴暗面. ...

  4. sql中NULL的问题

    sql中NULL的问题   今天一不小心在sql中写出以下脚本 select defaultPositionId from TableName where UserId=1100528 and def ...

  5. Python读写Json文件

    一个小例子,使用Json配置文件 # -*- coding: utf-8 -*- import json import time def store(data): with open('data.js ...

  6. php __FILE__,__CLASS__等魔术变量,及实例

    今天突然看到几个自己不认识的魔术变量 不知道怎么用于是就上网查了一下,看到了这篇博客,写的真不错,希望自己以后也能学会这样总结 张映 发表于 2010-12-13 分类目录: php 标签:php,  ...

  7. qt 多线程之间通讯

    问题描述:界面线程MainApp为主线程,工作线程MyThread为一子线程,从工作线程向主线程传递字符串用于在主线程中显示. Qt的信号与槽机制可以将任何继承自QObject类的对象捆绑在一起,使不 ...

  8. C# 动态Linq(结合反射)

      这篇文章决定对最近一个单机版Web程序用到的东西总结一下. 一.反射Linq之OrderBy 动态Linq结合反射对某字段排序: namespace 动态Linq { class Program ...

  9. [置顶] 玩转Eclipse — 自动注释插件JAutodoc

    代码注释是一种良好的编程习惯.不管对于他人还是自己,注释都有助于代码的阅读和理解.手动添加注释,是一个非常费时和繁琐的工作,严重影响软件开发效率,这也是绝大多数程序员不愿添加注释的主要原因.JAuto ...

  10. ZOJ3761(并查集+树的遍历)

    Easy billiards Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Edward think a g ...