代码请见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"/><LinearLayoutandroid:id="@+id/ll_title_content"android:layout_width="0dp"android:layout_height="40dp"android:orientation="vertical" android:layout_weight="4"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="20dp"android:background="#000000" /><TextViewandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// (1)创建要显示的文本内容String[] arr = { "header", "title", "content" };// (2)与使用ListActivity的最大区别:使用findViewById得到一个ListViewListView 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() {// 定义当某个选项被点击时的操作。@Overridepublic 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>>。

转载于:https://www.cnblogs.com/eaglegeek/p/4557954.html

AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表相关推荐

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

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

  2. UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery现在已经不使用了,API使用ViewPaper代替了,以后再学专研ViewPaper吧现在说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...

  3. UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView

    AdapterView的内容一般是包含多项相同格式资源的列表,常用的有5种AdapterView的子类: (1)ListView:简单的列表 (2)Spinner:下拉列表,给用户提供选择 (3)Ga ...

  4. AdapterView及子类

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

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

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

  6. android adapter 组件,Android UI - AdapterView 及其子类

    AdapterView AdapterView 是一个抽象类,其派生的子类在用法上十分相似: AdapterView 继承了 ViewGroup: AdapterView 及其子类的继承关系如下: A ...

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

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

  8. 2.5 UI组件-AdapterView及子类(疯狂android学习笔记)

    列表视图(ListView)和ListActivity ①直接使用ListView创建 ②让Activity继承ListActivity(相当于该activity显示的组件为ListView) 提示: ...

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

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

最新文章

  1. eclipse如何卸载adt插件
  2. ERROR 1820 (HY000): Unknown error 1820,ERROR 1046 (3D000):
  3. 为什么`[`比`子集更好?
  4. Tungsten Fabric SDN — DCI
  5. (J2EE学习笔记)解决Hibernate删除异常:deleted object would be re-saved by cascade
  6. 成功解决 利用plt.plot绘图时,横坐标出现浮点小数而不是整数的情况(坐标轴刻度)
  7. 四川航空签约神策数据,航司沉淀数据价值
  8. 韦博英语危机爆发,教培行业如何应对中年危机?
  9. pycharm建立启动器命令
  10. SpringBoot实战(五)之Thymeleaf
  11. 解析stm32的时钟
  12. java编写管理系统_用java编写学生信息管理系统
  13. 看unix高级编程时遇到apue.h找不到的问题
  14. 大数据之-Hadoop完全分布式_集群的启动和停止方式总结---大数据之hadoop工作笔记0039
  15. 支持向量机c++实现
  16. RoR vs. Java
  17. 如何用ps扣字体_PS如何抠文字 Photoshop抠字教程
  18. 企业架构之道(一)之企业信息化建设
  19. 嵌入式单片机STM32原理及应用
  20. 多项式展开的逆过程的MATLAB实现

热门文章

  1. 输出链表倒数第K个节点
  2. 移远NB-IOT BC28模块模组简介和实际应用方向详解
  3. s3c6140 UART驱动设计
  4. JUC并发编程六 并发架构--偏向锁
  5. springmvc二十七:springmvc-ResponseBody与ResponseEntity
  6. 阿里七层流量入口 Tengine硬件加速探索之路
  7. 我与前端 | 因兴趣起源
  8. Ajax实现--jQuery
  9. emmc boot1 boot2 partition
  10. Visual Studio Debug和Release的区别及obj的作用