安卓开发之ListAdapter(一)
Adapter常用来管理数据,是连接后端数据和前端显示的适配器接口,是数据和UI(view)之间一个重要的纽带。再常见的view(listview、gridview)等地方都需要用到adapter,下面我们来讲讲SimpleAdapter:
SimpleAdapter adapter = new SimpleAdapter(context, List<?extends Map<String,?>> data, resource, from, to);
context:上下文,指这个适配器所运行的视图,一般是指在那个activity(界面)
data:一个泛型的List,适配器的数据源(一般使用ArrayList封装Map、HashMap对象)
resource:适配器xml视图的引用路径
from:参数是一个数组对象,主要是将Map的键映射到列名,一一对应
to:将“from”的值一一对应显示,是一个R文件的数组,一般是你所要加载数据的控件的ID数组.
list.setAdapter(adapter);
代码示例:
java代码:
package com.sumzom.arrayadp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.com.sumzom.lv.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ArrayAdpActivity extends Activity{
private ListView ary_list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrayadp);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name","xoap");
list.add(map);
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("name","fnsjf");
list.add(map1);
SimpleAdapter adapter = new SimpleAdapter(
getApplicationContext(), list, R.layout.lv_item,
new String[]{"name"}, new int[]{R.id.tv} );
ary_list = (ListView) findViewById(R.id.ary_list);
ary_list.setAdapter(adapter);
}
}
xml代码:
activity绑定xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/ary_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
list item的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的第一个自定义适配器"
android:textColor="#ff00ff"/>
</LinearLayout>
</LinearLayout>
运行结果:

安卓开发之ListAdapter(一)的更多相关文章
- 安卓开发之ListAdapter(二)
今天我们来学习一下ArrayAdapter: ArrayAdapter是安卓中最简单的适配器.使用ArrayAdapter(数组适配器),需要把数据源存 放至数组里面来显示. •构造函数: publi ...
- 安卓开发之Toolbar
根据官网的教程,发现实现与预期不一致,查看相关资料自己整理了一下(官网开发文档:https://developer.android.com/training/appbar/setting-up.htm ...
- 安卓开发之UIwebview
web view在安卓开发中是比较常见的UI,像微信的新闻模块就采用了这个,他的作用越来越广,下面我把以前做的贴了出来,如果有更好的办法,希望大神不吝赐教哈,嘿嘿,纯代码来了: java代码 publ ...
- 安卓开发之activity详解(sumzom)
app中,一个activity通常是指的一个单独的屏幕,相当于网站里面的一个网页,它是对用户可见的,它上面可以显示一些控件,并且可以监听处理用户的时间做出响应. 那么activity之间如何进行通信呢 ...
- 安卓开发之json解析
1.从网页获取json返回字符串 public class ReadNet extends AsyncTask<URL, Integer, String> { @Override ...
- 安卓开发之viewpager学习(头条显示)
activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...
- 安卓开发之RecyclerView
RecyclerView是一个非常好用的控件,它的效果和ListView很相似,甚至可以说RecyclerView的出现是来取代ListView的 RecyclerView比ListView更加灵活, ...
- 安卓开发之APK安装之后自动在桌面上创建快捷图标
可以看到很多的APP在第一次运行之后就会弹出来一个Toast说什么快捷方式已创建,那么这个东西是怎么搞出来的呢 很简单就下面几句话,写在这儿以后好copy 先创建一个类 import android. ...
- 安卓开发之mqtt协议
首先物联网协议mqtt协议是基于tcp/ip协议的,使用了官方的mqttclient框架/**初始化mqttclient*/private void init() { try { //MQTT的连接设 ...
随机推荐
- Android图片选择器
1.概述 应公司项目需求,要做一个图片选择器,网上搜索了一些源码,我在别人的基础上进行了修改,另外页面也进行了重整,我的是先加载图片文件夹列表,然后再进入选择图片. 参考博客地址 ...
- MAT使用--转
原文地址: [1]http://ju.outofmemory.cn/entry/172684 [2]http://ju.outofmemory.cn/entry/129445 MAT使用入门 MAT简 ...
- SQL SERVER四舍五入你除了用ROUND还有其他方法吗?
引言 今天和测试沟通一个百分比计算方式时遇到一个问题, 我在存储过程里用到了强转CAST(32.678 AS DECIMAL(5,1)) 我认为该方式只会保留一位小数,我给测试的回复是我并没有用到四 ...
- Solr学习总结(三)Solr web 管理后台
前面讲到了Solr的安装,按道理,这次应该讲讲.net与数据库的内容,C#如何操作Solr索引等.不过我还是想先讲一些基础的内容,比如solr查询参数如何使用,各个参数都代表什么意思? 还有solr ...
- [Q&A] 在证书存储区中找不到清单签名证书
方案1:右击项目属性->签名->为 ClickOnce 清单签名,将勾掉的选项去掉. 方案2:在签名中创建一个新的签名. 方案3:记事本打开相应的 csproj 文件,调整节点值.< ...
- roleManager与角色管理授权
总览地址 https://msdn.microsoft.com/zh-cn/library/9ab2fxh0.aspx 其中基本概述是第一篇:了解角色管理 来自 <https://msdn.mi ...
- 【NopCommerce源码架构学习-二】单例模式实现代码分析
单例模式是是常用经典十几种设计模式中最简单的..NET中单例模式的实现也有很多种方式.下面我来介绍一下NopCommerce中单例模式实现. 我之前的文章就分析了一下nop中EngineContext ...
- webapi修改tt模板给字段添加JsonIgnore特性解决转换json循环引用问题
0.问题描述 EF生成的model带有导航属性,则json序列化会报循环引用错误,尝试如下 protected void Application_Start() { GlobalConfigurati ...
- EC笔记,第二部分:5.了解C++默默编写并调用哪些函数
5.了解C++默默编写并调用哪些函数 1.C++空类 C++会为一个空类建立以下函数 (1).默认构造函数 (2).默认拷贝构造函数 (3).析构函数 (4).赋值运算符(如果成员包含引用类型或con ...
- spring aop配置出错
Multiple annotations found at this line: - schema_reference.4: Failed to read schema document 'http: ...