View(视图)——ListView之 SimpleAdapter

一.概念

列表视图;用来显示多个可滑动项列表的ViewGroup;需要适配器Adapter 将集合中数据和每一个Item所对应的布局动态适配到ListView中进行显示。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity8">
11
12     <ListView
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:id="@+id/lv_2">
16     </ListView>
17
18 </LinearLayout>

activity.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5
 6     <ImageView
 7         android:layout_width="70dp"
 8         android:layout_height="70dp"
 9         android:src="@drawable/f1"
10         android:id="@+id/iv_2"/>
11     <LinearLayout
12         android:layout_width="0dp"
13         android:layout_height="match_parent"
14         android:layout_weight="1"
15         android:orientation="vertical"
16         android:layout_marginLeft="20dp"
17         android:gravity="center_vertical">
18
19         <TextView
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:text="名字=aaa"
23             android:id="@+id/tv_7"/>
24
25         <TextView
26             android:layout_width="match_parent"
27             android:layout_height="wrap_content"
28             android:text="内容=aaa"
29             android:id="@+id/tv_8"/>
30
31     </LinearLayout>
32
33 </LinearLayout>

simple_adapter

  1 package com.example.wang.testapp2;
  2
  3 import android.support.v7.app.AppCompatActivity;
  4 import android.os.Bundle;
  5 import android.widget.ListView;
  6 import android.widget.SimpleAdapter;
  7
  8 import java.util.ArrayList;
  9 import java.util.HashMap;
 10 import java.util.List;
 11 import java.util.Map;
 12 import java.util.Objects;
 13 import java.util.SimpleTimeZone;
 14
 15 public class TestActivity8 extends AppCompatActivity {
 16
 17     ListView lv_2;
 18
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22         setContentView(R.layout.activity_test8);
 23
 24         lv_2=(ListView)findViewById(R.id.lv_2);
 25
 26         //1.数据集合 Layout
 27
 28         List<Map<String,Object>> lm=new ArrayList<Map<String,Object>>();
 29
 30         Map<String,Object> map=new HashMap<String, Object>();
 31         map.put("img",R.drawable.f1);
 32         map.put("name","美食1");
 33         map.put("content","美食1的介绍");
 34         lm.add(map);
 35
 36         map=new HashMap<String, Object>();
 37         map.put("img",R.drawable.f2);
 38         map.put("name","美食2");
 39         map.put("content","美食2的介绍");
 40         lm.add(map);
 41
 42         map=new HashMap<String, Object>();
 43         map.put("img",R.drawable.f3);
 44         map.put("name","美食3");
 45         map.put("content","美食3的介绍");
 46         lm.add(map);
 47
 48         map=new HashMap<String, Object>();
 49         map.put("img",R.drawable.f4);
 50         map.put("name","美食4");
 51         map.put("content","美食4的介绍");
 52         lm.add(map);
 53
 54         map=new HashMap<String, Object>();
 55         map.put("img",R.drawable.f5);
 56         map.put("name","美食5");
 57         map.put("content","美食5的介绍");
 58         lm.add(map);
 59
 60         map=new HashMap<String, Object>();
 61         map.put("img",R.drawable.f6);
 62         map.put("name","美食6");
 63         map.put("content","美食6的介绍");
 64         lm.add(map);
 65
 66         map=new HashMap<String, Object>();
 67         map.put("img",R.drawable.f7);
 68         map.put("name","美食7");
 69         map.put("content","美食7的介绍");
 70         lm.add(map);
 71
 72         map=new HashMap<String, Object>();
 73         map.put("img",R.drawable.f8);
 74         map.put("name","美食8");
 75         map.put("content","美食8的介绍");
 76         lm.add(map);
 77
 78         map=new HashMap<String, Object>();
 79         map.put("img",R.drawable.f9);
 80         map.put("name","美食9");
 81         map.put("content","美食9的介绍");
 82         lm.add(map);
 83
 84         map=new HashMap<String, Object>();
 85         map.put("img",R.drawable.f10);
 86         map.put("name","美食10");
 87         map.put("content","美食10的介绍");
 88         lm.add(map);
 89
 90         //数组 key的数组
 91         String [] strings={"img","name","content"};
 92         int [] ids={R.id.iv_2,R.id.tv_7,R.id.tv_8};
 93
 94         //2.创建simpleAdapter
 95         SimpleAdapter simpleAdapter=new SimpleAdapter(this,
 96                 lm,R.layout.simple_adapter,strings,ids);
 97
 98         lv_2.setAdapter(simpleAdapter);
 99     }
100 }

activity.java

时间: 2024-08-08 17:23:45

View(视图)——ListView之 SimpleAdapter的相关文章

高级控件【安卓5】——滚动视图、列表视图[ListView、SimpleAdapter类]

滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_parent"> </ScrollView> 滚动试图指的是提供一个专门的容器,可以装下多于屏幕宽度的组件,而后采用拖拽的方式 显示所有在ScrollView中的组件 注意:滚动视图中只能有一个组件 列表视图[ListView.SimpleAdapter类] 1.ListView控

Android——列表视图 ListView(三)BaseAdapter

activity_activitybase.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_pa

ListView和SimpleAdapter

在Android中当我们需要在界面上显示列表内容,则要使用ListView控件. 本文主要介绍如何使用ListView控件来显示自己想要的内容. 本文的最终成果如下图所示,当用户点击ListView中的内容时,控制台将会输出相应的Item名字. 在layout当中,我们有两个xml文件,分别为 activity_main 和 simple_item 其中在 前者中创建了一个ListView 控件,在后者中则定义了ListView 中所要显示的列表项,在本项目中为两个TextView. activ

列表视图ListView之一

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. 一.简单应用 1.打开"res/layout/activity_main.xml"文件. (1)从工具栏向activity拖出1个列表视图ListView. (2)打开activity_main.xml文件. 完整代码如下: <?xml version="1.0" encoding="utf-8"?><Rela

列视图-Listview

如果程序需要显示不定数量的数据或者是动态变动的数据,比如联系人列表.相册,利用上一节的布局方式来实现将很不灵活,这种场景下最有效的展现视图是列表视图(ListView或Gridview).Listview和Gridview都继承自AbsListView,所以在使用上有类似的地方.下面具体介绍这两个视图的使用方法. Listview常用于展示一系列相似类型的数据,下面以一个简化的联系人列表来讲解Listview的基本用法. 主界面布局. main.xml 1.   <RelativeLayout

Android学习-列表视图ListView

一.简介: ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表.ListView的数据项,来自一个继承了ListAdapter接口的适配器. 二.新建一个包listview并新建ListViewActivity.java活动: 12345678 public class ListViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

Android 自定义View视图

创建全新的视图将满足我们独特的UI需求. 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图. 实现效果图: 源代码: 布局文件activity_main(其中CompassView继承View类): <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.

DNS主从服务,子域授权,view视图,日志系统,压力测试rsync配置

DNS主从服务,子域授权,view视图,日志系统,压力测试 DNS性能测试工具queryperfDNS查询过程: DNS主从建立: 环境: 主服务器:10.140.165.93 从服务器:10.140.165.169 关闭防火墙,关闭selinux. 主服务器建立: [[email protected] ~]# yum -y install bind-util bind #安装bind服务 [[email protected] ~]# vim /etc/named.conf #编辑主配置文件 o

Atitit.code&#160;base&#160;view&#160;视图的实现原理

Atitit.code base view 视图的实现原理 1. 视图的执行算法:1 2. 不可更新的视图:1 3. 关于视图的可插入性:insert2 4. 视图定义3 5. 调用3 1. 视图的执行算法: 存在两种执行算法: 1.  Merge:合并的执行方式,每当执行的时候,先将我们视图的sql语句与外部查询视图的sql语句,混合在一起,最终执行: 2.  Temptable:临时表模式,每当查询的时候,将视图所使用的select语句生成一个结果的临时表,再在当前的临时表内进行查询. 指的

linux下DNS主从复制,view视图,转发,子域授权

我们知道相同网段内各网络设备之间是基于mac通信,而跨网络的不同主机之间是基于IP地址通信.随着世界主机数量爆炸式的增长,对于记住数目众多IP和想访问未知对方IP的主机成为一个痛点.通过基于人们熟知的文字访问主机应运而生. DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析). DNS是一项