ListView与SimpleAdapter
来源:互联网 发布:fastjson与json 编辑:程序博客网 时间:2024/05/19 13:59
SimpleAdapter的基本特点
- SimpleAdapter的特点是使用List<Map> 的数据源格式,每个列表项的数据必须使用Map<String, ?>表示,其中,Map的Key可以自由命名,Value表示值,即需要被显示在列表项上的数据。
- SimpleAdapter可以自由设计列表项的布局。
- SimpleAdapter只能适用于每个列表项的外观都是一样的应用场景。
具体实现代码
1. 模板 contact_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" android:padding="10dp" > <ImageView android:id="@+id/iv_contact_item_photo" android:layout_width="36dp" android:layout_height="36dp" android:contentDescription="@null" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_contact_item_name" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignTop="@+id/iv_contact_item_photo" android:layout_toRightOf="@+id/iv_contact_item_photo" android:paddingLeft="4dp" android:text="@string/app_name" android:textSize="18sp" /> <TextView android:id="@+id/tv_contact_item_number" android:layout_width="match_parent" android:layout_height="16dp" android:layout_below="@+id/tv_contact_item_name" android:layout_toRightOf="@+id/iv_contact_item_photo" android:paddingLeft="4dp" android:text="@string/app_name" android:textSize="14sp" /></RelativeLayout>
2.MainActivity
package cn.tedu.listview_simpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity { /** * Adapter:适配器,用于将数据映射到View上的工具 */ private SimpleAdapter adapter; /** * ListView:用于显示列表数据的View */ private ListView lvContacts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvContacts = (ListView) findViewById(R.id.lv_contacts); // 上下文对象 Context context = this; // 数据集合 List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); Map<String, Object> item; // 向集合中添加数据 item = new HashMap<String, Object>(); item.put("name", "梅西"); item.put("number", "1234561"); item.put("photo", R.drawable.headimage01); data.add(item); item = new HashMap<String, Object>(); item.put("name", "内马尔"); item.put("number", "1234562"); item.put("photo", R.drawable.headimage02); data.add(item); item = new HashMap<String, Object>(); item.put("name", "苏亚雷斯"); item.put("number", "1234563"); item.put("photo", R.drawable.headimage03); data.add(item); item = new HashMap<String, Object>(); item.put("name", "C.罗纳尔多"); item.put("number", "1234564"); item.put("photo", R.drawable.headimage04); data.add(item); item = new HashMap<String, Object>(); item.put("name", "本泽马"); item.put("number", "1234565"); item.put("photo", R.drawable.headimage05); data.add(item); item = new HashMap<String, Object>(); item.put("name", "贝尔"); item.put("number", "1234566"); item.put("photo", R.drawable.headimage06); data.add(item); item = new HashMap<String, Object>(); item.put("name", "伊内斯塔"); item.put("number", "1234567"); item.put("photo", R.drawable.headimage07); data.add(item); // 显示数据的模板 int resource = R.layout.contact_item; // 通过键,查找相应的数据 String[] from = { "name", "number", "photo" }; // 数据放入的位置 int[] to = { R.id.tv_contact_item_name, R.id.tv_contact_item_number, R.id.iv_contact_item_photo }; // 适配器 adapter = new SimpleAdapter(context, data, resource, from, to); // 为列表项配置adapter lvContacts.setAdapter(adapter); }}
效果图如下:
0 0
- SimpleAdapter与ListView
- ListView与 simpleadapter 。baseadapter
- ListView与SimpleAdapter
- ListView 与SimpleAdapter
- SimpleAdapter与ListView的使用
- ListView与SimpleAdapter的使用
- ListView(SimpleAdapter)
- ListView SimpleAdapter
- listview simpleadapter
- ListView SimpleAdapter
- ListView--SimpleAdapter
- Android之ListView与SimpleAdapter的使用
- ListView 与 SimpleAdapter的简单用法
- 【Android】ListView与SimpleAdapter适配器的使用
- ListView与SimpleAdapter实现图文列表项
- ListView的使用----ArrayAdapter与SimpleAdapter
- Android控件之ListView与SimpleAdapter
- ListView(2)SimpleAdapter与SimpleCursorAdapter详解
- 与iOS的初吻---方法命名与调用
- HDU 2719 The Seven Percent Solution (printf输出%25)
- 母牛生小牛
- Linux下锐捷客户端v1.31安装与登陆
- Python与自然语言处理(一)搭建环境
- ListView与SimpleAdapter
- hdu5365Shortest Path (floyd)
- jstack dump 线程说明
- 解析xml dom4j
- 【权限管理】基础原理篇
- 教你打造广告位的循环播放
- BZOJ3668 NOI2014 起床困难综合症 题解&代码
- CDOJ 1091 秋实大哥の恋爱物语 KMP
- BZOJ_P3450&Tyvj_P1952 Easy(概率期望动态规划)