安卓开发之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的连接设 ...
随机推荐
- AngularJS之高级Route【三】(八)
前言 我们知道默认的路由提供(Route Provider)在复杂的应用程序中是不太适合应用场景,它存在诸多限制,所以在Angular 1.2之后此时我们不得不将路由提供作为一个单独的模块当我们需要使 ...
- 关于SubSonic3.0插件使用SqlQuery或Select查询时产生的System.NullReferenceException异常修复
早上在编写执行用例时,突然爆异常System.NullReferenceException: 未将对象引用设置到对象的实例 执行代码:
- SubSonic3.0.0.4.3源码包与调用Dll
版本修改历史 3.0.0.4.3版修复了下面问题: 修正多表关联查询时,使用左关联和右关联出错问题修正DbDataProvider.cs类的ToEnumerable函数打开数据库链接后没有关闭的问题添 ...
- [Keras] mnist with cnn
典型的卷积神经网络. Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0. ...
- 1Z0-053 争议题目解析520
1Z0-053 争议题目解析520 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 520.Which of the following are not disabled by de ...
- norflash驱动编写笔记
[部分转自]http://blog.csdn.net/ziyiyunmen/article/details/9744901 1. 读数据 md.b 0 2. 读ID NOR手册上: 往地址555H写A ...
- js实现String.Fomat
引言 拼接字符串用习惯了C#的String.Format.今天看别人的代码在js中也封装了一个js的String.Format,用来拼接字符串和DOM. js实现和调用String.Format St ...
- 测试为什么Low
你从来没有因为一个歌手不会写曲填词而说歌手很Low! 你从来没有因为一个演员不会摄影.唱歌而说演员很Low! 你从来没有因为一个记者不会摄影,拍照而说记者很Low! 你从来没有因为一个美食家不会烧菜, ...
- [.NET逆向] .net IL 指令速查(net破解必备)
.net的破解比较特殊,很多人看见IL就头疼,最近在研究的时候发现了这个东东 相信对广大学习net破解的人一定有帮助 .对上指令表一查,跟读原代码没什么区别了, 名称 说明 Add 将两个值相加并将结 ...
- C#的Socket实现UDP协议通信
今天稍花化了一点时间,利用C#的Socket验证了UDP的通信,为接下来特地利用UDP做个分布式的通信仿真系统打下基础.众所周知,UDP 就是用户数据报协议,在互联网参考模型的第四层——传输层.与TC ...