1. package test.simpleAdapter;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import com.example.simpleadapters.R;
  9.  
  10. import android.app.Activity;
  11. import android.os.Bundle;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14.  
  15. public class ListContent extends Activity {
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. this.setContentView(R.layout.list_content);
  21. //-----------------------------------------
  22. //为simpleAdapter构造数据
  23. String []title = {"排山倒海","葵花点穴手","如来神掌"};
  24. String []tips = {"威力巨大给对手造成巨大内伤,即使对手逃逸也会无疾而终","短暂制瘫对方,瞬间废除对手活动能力,即可为所欲为","有效杀伤力达几百米寸草不生,但灰尘太大不环保"};
  25. int []icon = {R.drawable.psdh,R.drawable.khdxs,R.drawable.rlsz};
  26. String []time ={"14:30","14:20","14:10"};
  27. //---------------------------------------
  28. //构造玩上述数据后,开始构造它们xml布局
  29. //---------------------------------------
  30. //将数据打包成MAP对象
  31. List<Map<String,Object>> map_content = new ArrayList<Map<String,Object>>();
  32. for(int i = 0; i < title.length;i++){
  33. Map map = new HashMap<String, Object>();
  34. //打包方式 key value键值对形式
  35. map.put("title", title[i]);
  36. map.put("tips", tips[i]);
  37. map.put("icon", icon[i]);
  38. map.put("time", time[i]);
  39. //------------------------------------
  40. map_content.add(map);
  41. }
  42. //----------------------------------------
  43. //生成xml布局资源ID引用
  44.  
  45. int []xmlres = {R.id.avatar,R.id.list_title,R.id.list_tips,R.id.list_time};
  46. //生成键素组
  47. String []mapkey = {"icon","title","tips","time"};//与上面的xmlres数组的资源ID对应
  48. //map对象构造完毕,接下来生成simpleAdapter对象
  49. //构造simpleAdapter所需参数说明:context窗口对象,盛装map的list,列表栏目布局资源ID的引用,list中的map中对应的键名称,键名称对应的控件资源ID;
  50. SimpleAdapter sAdapter = new SimpleAdapter(ListContent.this, map_content, R.layout.simple_adapter_list, mapkey, xmlres);
  51. ListView list = (ListView)findViewById(R.id.main_list);
  52. list.setAdapter(sAdapter);//设置资源
  53.  
  54. }
  55.  
  56. }

xml:

  1. //list_content
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <ListView
  8. android:id="@+id/main_list"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent">
  11.  
  12. </ListView>
  13.  
  14. </LinearLayout>
  1. //simple_adapter
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="horizontal" >
  11. <ImageView
  12. android:id="@+id/avatar"
  13. android:layout_width="match_parent"
  14. android:layout_height="70dp"
  15. android:layout_weight="0.7"/>
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="0.3"
  20. android:orientation="vertical" >
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="horizontal" >
  25. <TextView
  26. android:id="@+id/list_title"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="0.2"/>
  30. <TextView
  31. android:id="@+id/list_time"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_weight="0.8"/>
  35. </LinearLayout>
  36. <TextView
  37. android:id="@+id/list_tips"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"/>
  40. </LinearLayout>
  41. </LinearLayout>
  42.  
  43. </LinearLayout>

第二章实例:SimpleAdapter结合listview实现列表视图的更多相关文章

  1. 通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏--核心代码--猜测次数 可复制直接使用 娱乐 可封装 函数

      猜数字游戏--核心代码--猜测次数   #猜数字--核心代码--猜测次数 number=33 amount=3 count=0 while count<=amount: conversion ...

  2. 第二章实例:ArrayAdapter结合ListView列表视图

    package mydefault.packge; import com.example.codeview.R; import android.app.Activity; import android ...

  3. 第二章实例:ActionBar结合Fragment显示

    package tab.test; import com.example.actionbar_tab.R; import android.app.ActionBar; import android.a ...

  4. 第二章实例:Android窗口菜单显示

    package test.main.cls; import com.example.popupwindow.R; import android.app.Activity; import android ...

  5. 第二章实例:动态生成View控件例子---小球跟随手指滑动

    package mydefault.packge; import android.app.Activity; import android.content.Context; import androi ...

  6. 使用SimpleAdapter创建ListView

    通过ArrayAdapter实现Adapter虽然简单.易用,但ArrayAdapter的功能比较有限.它的每个列表只能是TextView.如果开发者需呀实现更复杂的列表项,则可以考虑使用Simple ...

  7. HTML第二章:列表,表格,媒体元素

    第二章:列表,表格,媒体元素 列表:有三种,有序列表,无序列表,定义列表 1.有序列表:<ol></ol>            列表项:<li></li&g ...

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

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

  9. [ABP教程]第二章 图书列表页面

    Web应用程序开发教程 - 第二章: 图书列表页面 关于本教程 在本系列教程中, 你将构建一个名为 Acme.BookStore 的用于管理书籍及其作者列表的基于ABP的应用程序. 它是使用以下技术开 ...

随机推荐

  1. 在本地调试微信项目(C#)

    之前一人负责微信的项目,那时2014年LZ还没毕业..啥都不懂,为此特别感谢@SZW,没有你的框架,我可能都无从下手 当时做项目最麻烦的就是调试,因为很多页面都要使用 网页授权获取用户信息 在电脑上打 ...

  2. asp.net缓存(三)

    Asp.net应用程序数据缓存 System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和 ...

  3. Memcached内存管理模型分析

    Memcached 是一个高性能的分布式内存对象缓存系统,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而减轻RDBMS的负担,提高服务的速度.提升可扩展性.本文将基于memcached1.4 ...

  4. CDZSC_2015寒假新人(1)——基础 h

    Description Ignatius was born in a leap year, so he want to know when he could hold his birthday par ...

  5. java开发webservice

    第一部分:相关下载配置 1.开发环境   eclipse-jee-mars-2-win32-x86_64.zip  http://www.eclipse.org/downloads/index-pac ...

  6. EditText判断用户输入完成,然后进行操作解决方案

    private Timer timer = new Timer(); private final long DELAY = 1000; // in ms EditText editTextStop = ...

  7. Zend Studio 文件头和方法注释设置

    在zend studio中选择窗口->首选项->PHP–>编辑器 –>模板 –>新建 然后添加 funinfo或fileinfo 模板代码根据下边定义的COPY过去就可以 ...

  8. ni

    坚强歌词 马天宇 - 坚强 天使的翅膀挥动着的光芒一路走来学会了坚强每一次你努力认真的模样让我很欣赏 雨天的路上会有一缕阳光温暖被淋湿的希望再小的河也能汇成海洋让我去远航 一路上陪伴我的目光是最感动的 ...

  9. BC 65 game

    主持人一直某个数字在1到n范围,假设甲乙已经知道,甲先猜乙后,都采用最优策略,主持人说偏大还是偏小,不断缩小范围,问最后乙能会获胜的X的取值的个数. 如果n为奇数,那么仅当x=n/2乙必然获胜,若为奇 ...

  10. SQL Server索引设计 <第五篇>

    SQL Server索引的设计主要考虑因素如下: 检查WHERE条件和连接条件列: 使用窄索引: 检查列的选择性: 检查列的数据类型: 考虑列顺序: 考虑索引类型(聚集索引OR非聚集索引): 一.检查 ...