Activity和item:

Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </RelativeLayout> item:
<?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="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv_photo"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/photo3"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名字"
android:textSize="22sp"
android:layout_gravity="center_vertical"
/> </LinearLayout>

java:

package com.itheima.arraysimple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); String[] objects = new String[]{
"小志",
"小志的儿子",
"萌萌"
}; ListView lv = (ListView) findViewById(R.id.lv); //ArrayAdapter只能够处理一种数据类型String,做了高度的封装。
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv_name, objects)); //集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("photo", R.drawable.photo1);
map1.put("name", "小志的儿子");
data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("photo", R.drawable.photo2);
map2.put("name", "小志");
data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("photo", R.drawable.photo3);
map3.put("name", "赵帅哥");
data.add(map3); //SimpleAdapter,item_listview是item,
//new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}指定photo放入R.id.iv_photo组件,name放入R.id.tv_name组件
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview,
new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));
} }

android 71 ArrayAdapter和SimpleAdapter的更多相关文章

  1. android 适配器 ArrayAdapter,SimpleAdapter的学习

    今天认真看了下android适配器,学习了下它的使用方法. 一,ArrayAdapter ArrayAdapter 比较简单,只可以存放一行文本信息.下面是简单的实现 private ListView ...

  2. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

  3. 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter

    Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...

  4. android ArrayAdapter BaseAdapter SimpleAdapter使用讲解

    不是我针对谁,我只想针对新手玩家. 不清楚Adapter作用的可以看一下http://www.cnblogs.com/zhichaobouke/p/5798672.html (括号里的内容都是我主观添 ...

  5. [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段

      收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...

  6. 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理

    在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...

  7. ArrayAdapter与SimpleAdapter的使用

    在使用ListView中我们使用到adapter,android中为我们不仅提供了BaseAdapter类来让我们自定义自己的Adapter,还为我们提供了ArrayAdapter以及SimpleAd ...

  8. ArrayAdapter、SimpleAdapter简单用法

    1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects)   context:上下 ...

  9. ArrayAdapter、SimpleAdapter和BaseAdapter示例代码

    import android.content.Context; import android.util.Pair; import android.view.View; import android.v ...

随机推荐

  1. 堆排序 海量数据求前N大的值

    最(大)小堆的性质: (1)是一颗完全二叉树,遵循完全二叉树的所有性质. (2)父节点的键值(大于)小于等于子节点的键值 堆的存储 一般都用数组来表示堆,i结点的父结点下标就为(i – 1) / 2. ...

  2. python多线程 批量下补丁

    一个一个下载 要2个多小时.就直接起了个线程池.效果明显.import urllib2 from urlparse import urlparse uri = 'http://******/patch ...

  3. redis基本命令的演示:

    import redis r = redis.Redis(host='127.0.0.1', port=6379,db = 0) #查看匹配redis的数据 r.keys() #查看redis的大小 ...

  4. bzoj 2852: 强大的区间 辗转相除

    2852: 强大的区间 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 12[Submit][Status][Discuss] D ...

  5. OSI/RM网络7层体系

    转自OSI/RM网络7层体系 1 物理层 这是整个OSI参考模型的最低层,它的任务就是提供网络的物理连接.所以,物理层是建立在物理介质上(而不是逻辑上的协议和会话),它提供的是机械和电气接口.主要包括 ...

  6. Intellij IDEA快速编写代码经验总结

    sout=System.out.println(); soutp=System.out.println(""); soutv=System.out.println("变量 ...

  7. 【网络流24题】No.21 (最长 k 可重区间集问题 最长不相交路径 最大费用流)

    [] 输入文件示例input.txt4 21 76 87 109 13 输出文件示例output.txt15 [分析] 直接co题解好了,写得挺全.. [建模方法] 方法1 按左端点排序所有区间,把每 ...

  8. Maven学习:Eclipse使用maven构建web项目(转)

    Maven学习:Eclipse使用maven构建web项目(转) 8.更改class路径:右键项目,Java Build Path -> Source 下面应该有4个文件夹.src/main/j ...

  9. 原始启动log&新log

    root@Taiyear:/# U-Boot 1.1.3 (Dec 27 2013 - 09:14:28) SoC:MediaTek MT7620 DRAM:  Memory Testing..655 ...

  10. Http状态码完整说明

    在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样, 一些常见的状态码为: 200 - 服务器成功返 ...