Android开发之适配器
来源:互联网 发布:淘宝网 医用弹力祙 编辑:程序博客网 时间:2024/06/05 02:23
android提供了的两种适配器,ArrayAdapter和SimpleAdapter
1.ArrayAdapter
package com.linxiaosheng.listview;import com.linxiaosheng.R;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class MyListView extends Activity {private ArrayAdapter<String> arrayAdapter;//private SimpleAdapter simpleAdapter;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.listview);listView=(ListView)findViewById(R.id.listView1);/** <span style="white-space:pre"></span>*context:上下文<span style="white-space:pre"></span>* resource:数据视图<span style="white-space:pre"></span>* 数据源:数组或list集合 <span style="white-space:pre"></span>*/<span style="white-space:pre"></span>String arr[]={"广州","深圳"};<span style="white-space:pre"></span>arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr );<span style="white-space:pre"></span>listView.setAdapter(arrayAdapter);}}完成效果:
2.SimpleAdapter
package com.linxiaosheng.listview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import com.linxiaosheng.R;public class MyListView extends Activity {private SimpleAdapter simpleAdapter;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.listview);listView=(ListView)findViewById(R.id.listView1);/**<span style="white-space:pre"></span>* context:上下文<span style="white-space:pre"></span>* data:数据源(List<? extends Map<String,Object>>data)一个map组成的list集合<span style="white-space:pre"></span>* <span style="white-space:pre"></span>每一个map的键值对都会去对应listview的一行 <span style="white-space:pre"></span>* <span style="white-space:pre"></span>每一个map键值对中的健必须包含在from数组中所指定的健<span style="white-space:pre"></span>* resource:列表项的布局文件ID<span style="white-space:pre"></span>* form:map中的健名 <span style="white-space:pre"></span>* to:绑定数据视图的ID,与forom成对应关系<span style="white-space:pre"></span>*/<span style="white-space:pre"></span>simpleAdapter=new SimpleAdapter(this, getSimpleAdapterData(), R.layout.simplelistview,new String[]{"name","age"}, new int[]{R.id.imageView1,R.id.textView1});<span style="white-space:pre"></span>listView.setAdapter(simpleAdapter);}public List<Map<String,Object>> getSimpleAdapterData(){List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();for(int i=0;i<10;i++){Map<String,Object> map=new HashMap<String, Object>();map.put("name", R.drawable.ic_launcher);map.put("age", 22);list.add(map);}return list;}}
<?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="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="简单适配器" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
完成效果:
0 0
- Android开发之适配器
- Android开发之适配器ListView
- Android开发之ListView适配器优化
- Android开发学习之常见适配器总结
- Android开发之适配器模式初探
- android开发之ListView GridView适配器架构
- android开发笔记之打造终极适配器
- android开发之适配器相关介绍
- Android开发之实现RecyclerView万能适配器
- Android开发之定义万能适配器
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- Android开发之ListView 适配器(Adapter)优化
- 利用Redis实现亿级别用户登录统计(活跃度以及登录次数统计)
- 08_Android中的SimpleAdapter的使用
- Java基础—网络编程(一)
- servlet request 转发, 原文件和转发文件输出顺序
- 如何将关系型数据表转换至hbase数据表
- Android开发之适配器
- 设计模式(十九)之Mediator(中介者)
- Android之Handler详解(一)
- leetcode刷题,总结,记录,备忘137
- js中操作符 || 和 &&
- Android之Handler详解(二)
- cocos2d-x初探
- 学习笔记-基础知识1-java编程概述
- samba服务实现linux和windows文件共享