适配器之SimpleAdapter
前言:
在写适配器时,SimpleAdapter会经常使用到,虽然他比ArrayAdapter复杂,但是也提供了更多的功能
正文:
我们接下来先从SimpleAdapter中较为简单的显示两行文本开始讲解
来直接上代码
在主活动中添加
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
我们在自己写一个布局文件
因为我们写的目的是显示两行文本,在上面显示数据,下面显示简介
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
android:text="数据"
android:textSize="10sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/intro"
android:text="简介"
android:textSize="20sp"/> </LinearLayout>
接下来是Java代码:
package com.example.administrator.testsimpleadapter; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; public class MainActivity extends AppCompatActivity {
private ListView listview;
private SimpleAdapter adapter;
private List<Map<String,Object>>list;
private Map<String,Object>map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
listview=(ListView)findViewById(R.id.listview);
//初始化数据
list=new ArrayList<Map<String,Object>>();
for (int i=0;i<=40;i++){
map=new HashMap<String,Object>();
map.put("数据","数据为"+i);
map.put("简介","简介为"+i);
list.add(map);
}
//初始化适配器
String[]form={"数据","简介"};
int[]to={R.id.date,R.id.intro};
adapter=new SimpleAdapter(MainActivity.this,list,R.layout.simple_item,form,to);
listview.setAdapter(adapter);
}
}
初始化AdapterView
17行,26行,39行都是和AdapterView有关前两个是为了初始化AdapterView,最后一个是为了把Adapter添加到listview中显示
初始化数据源
接下来讲一下初始化数据,SimpleView使用的数据都是用Map<String,?>其键必须为String型,值可以为任意,所以在初始化list是都用到
private List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
初始化Adapter
接下来是初始化SimpleAdapter适配器,其参数一是上下文对象,参数二是数据源,参数三是布局资源,参数四为String型数组,其值为数据源中的键,参数四int型数组为布局文件中控件的id,与参数四对应,比如第一个控件显示参数四数组中的第一个键的值
下面介绍SimpleAdapter图文混排
因为是图文混排所以必须有显示图片和文字的地方,在layout中添加simple_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView
android:padding="4dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/img"
android:src="@drawable/qq"
android:scaleType="fitXY"/>
<TextView
android:padding="4dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="名字"
android:layout_toRightOf="@id/img"
android:textSize="20sp"
/>
<TextView
android:id="@+id/intro"
android:padding="4dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="简介"
android:textSize="22sp"
android:layout_toRightOf="@id/img" android:layout_alignBottom="@id/img"
android:layout_alignLeft="@id/name"/>
</RelativeLayout>
接下来是Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
private ListView listview;
int[]imag={R.drawable.qq};
String[]name={"聊天"};
private List<Map<String,Object>>list;
private Map<String,Object>map;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化适配器view
listview=(ListView)findViewById(R.id.listview);
//初始化数据源
list=new ArrayList<Map<String,Object>>();
for (int i=0;i<name.length;i++){
map=new HashMap<String,Object>();
map.put("img",imag[i]);
map.put("name",name[i]);
map.put("intro","这是款不错的"+name[i]+"软件");
list.add(map);
}
String[]form={"img","name","intro"};
int []to={R.id.img,R.id.name,R.id.intro};
//设置适配器
adapter=new SimpleAdapter(MainActivity.this,list,R.layout.pic_item,form,to);
listview.setAdapter(adapter);
}
}
和上面的两行文字显示差不多,就是在第34行代码中另外加了ImageView的id,显示的内容也有图片
适配器之SimpleAdapter的更多相关文章
- C++STL模板库适配器之优先级队列
目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...
- C++STL模板库适配器之queue队列
目录 适配器之队列 一丶队列简介 二丶队列(queue)代码操作 1.常用方法 适配器之队列 一丶队列简介 队列是先进先出的数据结构. 在STL中使用 queue表示. 底层使用的是序列容器deque ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
- [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...
- 函数对象适配器之ptr_fun的使用示例
//============================================================================ // Name : CopyInts4.c ...
- 容器适配器之priority_queue
template <class T, class Container = vector<T>, class Compare = less<type ...
- 容器适配器之stack
参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...
- 容器适配器之queue
转载http://blog.csdn.net/thefutureisour/article/details/7751846容器适配器容器适配器其实就是一个接口转换装置,使得我们能用特定的方法去操作一些 ...
- C++STL模板库适配器之stack容器
目录 适配器 一丶适配器简介 二丶栈(stack)用法 1.栈的常用方法 适配器 一丶适配器简介 Stl中的适配器,有栈 (stack) 队列 queue 根priority_queue 适配器都是包 ...
随机推荐
- redis 高级学习和应用场景
redis 高级学习 1.redis 复制 2.redis 集群 3.哨兵机制 4.spring 与哨兵结合 5.数据恢复与转移 6.redis 的阻塞分析 redis 实战 1. 数据缓存(热点数据 ...
- 【转】android之在activity中控制另一个activity的UI更新_如何在activity之间传递handler
来自:http://blog.csdn.net/jason0539/article/details/18055259 遇到一个问题,需要在一个activity中控制另一个acitivity做一些更新, ...
- AWS-DDNS
1. DDNS 2. 在 Linux 实例上设置动态 DNS 2.1 Ubuntu 2.2 Amazon Linux 2 2.3 Arch Linux 2.4 其他Linux系统 3. 更多相关 1. ...
- Python 之并发编程之进程下(事件(Event())、队列(Queue)、生产者与消费者模型、JoinableQueue)
八:事件(Event()) # 阻塞事件: e = Event() 生成事件对象e e.wait() 动态给程序加阻塞,程序当中是否加阻塞完全取决于该对象中的is_set() [默认返回值 ...
- uniGUI之MASK遮罩(22)
在页面进行后台数据库操作的时候,不想 用户再进行 页面上的 其他操作,这时候就要 将页面 遮罩.例如UniDBGrid有LoadMask属性. 1]使用ScreenMask函数 2]JS调用 3]一个 ...
- TensorFlow Serving简介
一.TensorFlow Serving简介 TensorFlow Serving是GOOGLE开源的一个服务系统,适用于部署机器学习模型,灵活.性能高.可用于生产环境. TensorFlow Ser ...
- c++存储区域
来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...
- js网页拉起支付宝支付
js网页唤起支付宝进行支付 在做uni-app项目中,打包成 ios App的时候,为了绕过苹果支付的审核,所以用的 webview 加载支付宝的网页支付,进行付款 具体实现流程: 前端通过 url ...
- Mongo2Go 介绍
Mongo2Go(https://github.com/Mongo2Go/Mongo2Go )是最新的MongoDB二进制文件的托管包装, 它针对.NET Standard 1.6(对于.NET 4. ...
- JSP上传图片程序
1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO ...