Android 简单适配器(SimpleAdapter)
1.介绍
2.简单适配器的实现方法
3.XML文件
(1)主页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </ListView> </LinearLayout>
(2)子项布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" /> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@mipmap/img01" />
</LinearLayout>
4.java后台
package com.lucky.test29simpleadapter; 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 { ListView listView; //列表视图
SimpleAdapter simpleAdapter; //简单适配器可以将图片和文件组合起来
List<Map<String,Object>> list1;
//Android studio工程将图片转变为的数字
int[] images={R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06,R.mipmap.img07,R.mipmap.img08};
String[] contentstr={"虎扑","头条","QQ","微信","百度","起点","美团","滴滴"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listview1);
list1=new ArrayList<>(); //实例化list1
for (int i = 0; i <images.length ; i++) {
Map<String,Object> map=new HashMap<>(); //实例化map
map.put("001",images[i]); //给map添加值,参数1为代号,参数2为数据值
map.put("002",contentstr[i]);
list1.add(map);
}
//实例化simpleAdapter,并设置参数,参数2为数据,参数3为子项的布局文件,参数4为数据的代号,参数5为组件的id
simpleAdapter=new SimpleAdapter(MainActivity.this,list1,R.layout.itemsimple,new String[]{"001","002"},new int[]{R.id.imageView,R.id.textView});
listView.setAdapter(simpleAdapter); //设置适配器 }
}
5.效果图
Android 简单适配器(SimpleAdapter)的更多相关文章
- Android简易实战教程--第十八话《ListView显示,简单的适配器SimpleAdapter》
本篇介绍Listview的显示,对于listview有许多的适配器,如ArrayAdapter,BaseAdapter,SimpleAdapter等等.本篇先热身一下,介绍最简单的SimpleAdap ...
- android 适配器simpleadapter和baseadapter区别
android 适配器 simpleadapter 和 baseadapter 设计网络程序或者数据处理显示程序的时候,常常会使用 simpleadapter 和baseadapter 来实现. ad ...
- Android 数据适配器
把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上. arrayAdapter(数组适配器): 用于绑定一些格式单一的数据,数据源:数据或者集合. private Li ...
- [置顶] Android常用适配器控件
Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...
- Android简单文件浏览器源代码 (转)
Android简单文件浏览器源代码 (转) activity_main .xml <LinearLayout xmlns:android="http://schemas.android ...
- Android进阶笔记10:Android 万能适配器
1. Android 万能适配器 项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都 ...
- Android简单逐帧动画Frame的实现(二)
Android简单逐帧动画Frame的实现 Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...
- Android进阶笔记09:Android 万能适配器
1. Android 万能适配器 项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都 ...
- Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)
GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下: 一直是通过自定义Adapter方式,在getView()方法中设置图片的显示 ...
随机推荐
- 【原创】12. MYSQL++之Template Query
1. 什么是Template Query 在我们实际的编程过程中,我们很容易碰到printf这类需要在运行时来决定到底打印出什么的函数,例如 printf(“hello %s”, sth); 在这个例 ...
- 【原创】9. MYSQL++中的Field、FieldNames以及FieldTypes类型
mysqlpp::Field其实使用的并不多,主要在于Result.h中ResultBase以及他的派生类型(UseQueryResult和StoreQueryResult)的几个获取下一个field ...
- 并发之AbstractQueuedLongSynchronize----AQS
一概述 谈论到并发,不得不谈论锁,而谈论到锁而言,又离不开ReentrantLock.ReentrantLock是锁锁的一种实现方式,对于锁而言,我们这里就需要讨论到AQS,即上面的AbstractQ ...
- inux php pdo mysql 扩展
今天在本机部署了一个pdo项目,发现一些问题,真没想到pdo mysql,不容易装啊,哈哈,我说的不容易,是因为php5.3以前版本,yum源里面根本没有.部署后就报,Undefined class ...
- web优化
一个小型的网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单.随着互联网业务的不断丰富,网站相关的技术经过 ...
- Log4J 配置 详解
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 3.Strings 字符串如何工作?----对缓冲区的理解。
修改Hello World程序向特定的人问好. #include <iostream> #include <string> int main() { std::string n ...
- MFC可视化 列表控件的使用
1.应该加入头文件 #include <Atlbase.h> 2.示例 类向导给列表控件绑定变量m_list DWORD dwExStyle=LVS_EX_FULLRO ...
- 编写高质量代码改善C#程序的157个建议——建议52:及时释放资源
建议52:及时释放资源 垃圾回收机制自动为我们隐式地回收了资源(垃圾回收器会自动调用终结器),那我们为什么要主动释放资源呢? private void buttonOpen_Click(object ...
- 服务器控件数据回发实现IPostBackDataHandler需注意的
我写的服务器控件(未完,模型如此) using System; using System.Collections.Generic; using System.Collections.Specializ ...