- 浏览: 84488 次
- 性别:
- 来自: 成都
最新评论
Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View
因为layout五花八门,很多数据都不能直接绑定上去,所以Android引入了Adapter这个机制作为复杂数据的展示的转换载体,所以各种Adapter只不过是转换的方式和能力不一样而已,没什么大不了的
不多说,今天来看下几种常用的Adapter:
因为layout五花八门,很多数据都不能直接绑定上去,所以Android引入了Adapter这个机制作为复杂数据的展示的转换载体,所以各种Adapter只不过是转换的方式和能力不一样而已,没什么大不了的
不多说,今天来看下几种常用的Adapter:
数据
源不同则适配器不同
1. String[]: ArrayAdapter
1. String[]: ArrayAdapter
- ArrayAdapter
- SimpleAdapter
- SimpleCursorAdapter
- SimpleExpandableListAdapter
- SimpleCursorTreeAdapter
ArrayAdapter:顾名思义,专门负责将数组结构的数据适配进view中的,最简单,常用于demo...⊙﹏⊙b汗...和Spinner ,AutoCompleteTextView 中
SimpleAdapter:从名字上看不出什么所以然,其他这个东西很给力,在正常情况下他的灵活性最好,扩展性也最强(ViewBinder) ListView 中
SimpleCursorAdapter:拥有上者的扩展性和灵活性,同时可以将Cursor进行适配
SimpleExpandableListAdapter:这玩意就漏也了,只能适配到TextView上,在简单的UI中可以为ExpandableListView提供数据
SimpleCursorTreeAdapter:这是Adapter的终极Boss...-_-|||... ViewBinder+Cursor+Expandable...三位一体
2. 数据库
Cursor: SimpleCursorAdapter
3. List<Map
<String,?>>: SimpleAdapter
ArrayAdapter 需要把数据放入一个数组以便显示;
android .R.layout.simple_list_item_1是系统 定义 好的布局文件 只显示一行文字
SimpleCursorAdapter 是和数据库有关的东西,而sqlite数据库所要求的 自动 增长的 id 必须是 _id这中形式
SimpleAdapter 能定义各种各样的布局出来,可以放上ImageView (图片),还可以放上Button(按钮),CheckBox(复选框)
Arrayadapter代码
- public class List1 extends Activity {
- private ListVeiw listView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listView=new ListVeiw(this);
- listView.setAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mStrings));
- setContentVieww(listView);
- }
- private String[] mStrings = {
- "Abbaye de Belloc" , "Abbaye du Mont des Cats" ,
- "Acorn" , "Adelost" , "Affidelice au Chablis" ,
- "Aisy Cendre" , "Allgauer Emmentaler" , "Alverca" ,
- "Ami du Chambertin" , "Anejo Enchilado" , "Anneau
- "Aragon" , "Ardi Gasna" , "Ardrahan" , "Armenian
- "Asadero" , "Asiago" , "Aubisque Pyrenees" , "Autun" ,
- "Babybel" , "Baguette Laonnaise" , "Bakers" , "Bal" };
- }
public class List1 extends Activity { private ListVeiw listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView=new ListVeiw(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); setContentVieww(listView); } private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Acorn", "Adelost", "Affidelice au Chablis", "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ami du Chambertin", "Anejo Enchilado", "Anneau "Aragon", "Ardi Gasna", "Ardrahan", "Armenian "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Babybel", "Baguette Laonnaise", "Bakers", "Bal"}; }
Simplecursoradapter代码
- public class List2 extends Activity {
- private ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listView=new ListView(this);
- Cursorc=getContentResolver().query(People.CONTENT_URI,
- null, null, null, null);
- startManagingCursor(c);
- ListAdapter adapter = new SimpleCursorAdapter(this,
- android.R.layout.simple_list_item_1,
- c,
- new String[] {People.NAME} ,
- new int[] {android.R.id.text1});
- listView.setAdapter(adapter);
- setContentView(listView);
- }
- }
public class List2 extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView=new ListView(this); Cursorc=getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(c); ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[] {People.NAME} , new int[] {android.R.id.text1}); listView.setAdapter(adapter); setContentView(listView); } }
Simpleadapter代码
- public class List3 extends ListActivity {
- List<Map<String, Object>> list;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- list = getListForSimpleAdapter(); //list为数据集合
- SimpleAdapter adapter = new SimpleAdapter(this, list,
- R.layout.item,
- new String[] { "BigText" , "LittleText" , "img" },
- new int[] { R.id.BigText,R.id.LittleText, R.id.img });
- //可以想成把一个放好数据的布局文件放入本界面中
- setListAdapter(adapter);
- }
- private List<Map<String, Object>> getListForSimpleAdapter() {
- List<Map<String,Object>> list=newArrayList<Map<String, Object>>( 3 );
- Map<String, Object> map = new HashMap<String, Object>();
- map.put( "BigText" , "Android" );
- map.put( "LittleText" , "Google phone." );
- map.put( "img" , R.drawable.n);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put( "BigText" , "Lenovo" );
- map.put( "LittleText" , "Ophone" );
- map.put( "img" , R.drawable.o);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put( "BigText" , "Droid" );
- map.put( "LittleText" , "Motorola" );
- map.put( "img" , R.drawable.droid);
- list.add(map);
- return list;
- }
- }
发表评论
-
转 android背景选择器selector用法汇总
2011-04-27 15:23 1319原址:http://blog.sina.com.cn/s/bl ... -
(转)编写Android自定义按钮
2011-04-27 11:07 1181原址:http://marshal.easymorse.com ... -
(转)android button 效果设计
2011-04-26 15:04 1369原址:http://ssd910.blog.163.com/b ... -
(转)消息机制细谈(message handler looper MessageQueue )
2011-04-12 11:20 1029在handler的简单用 ... -
解决:sqlite数据库含有\n的值或者sax解析xml有\n的值付给TextView,显示却不会换行
2011-03-17 23:00 2907遇到个问题,网上也有人问 从sqlite数据库取含有\n的值 ... -
资料分享 android api dev guid中文(Android开发指南中文版.doc)
2011-03-11 14:12 1406资料分享 android api dev guid中文 ... -
apk反编译问题总结(转)
2011-03-09 15:01 2737http://www.eoeandroid.com/thre ... -
PendingIntent和Intent的区别 转
2011-03-04 14:35 1210Notification notification ... -
转:动态添加/删除spinner菜单--ArrlayList与Widget依赖性
2011-02-24 14:28 1722package irdc.ex04_09; import a ... -
Dialog 2种显示方式
2011-02-23 17:35 1296建议用onCreateDialog(int)和 show ... -
转 【通知 Toast详细用法(显示view)】 【android Toast大全(五种情形)建立属于你自己的Toast】
2011-02-11 10:36 3028原文地址: http://www.pocketdigi. ... -
(转 Android UI学习 - Menu)
2011-02-09 14:19 1346标签:Android menu 原创作品,允许转载,转载时 ...
相关推荐
分别用simple Adapter和array Adapter实现listview的布局,用simple Adapter实现gridview的布局。
前端开源库-simple-array-diff简单数组diff,查找在两个数组或元素或对象之间显式添加和删除的元素。
A simple program that shows how a two-dimensional array works within a VB program.
选择集ss转VB array ,用来调用常用的vla-*****函数
delphi中String,PChar,PByte,Array of Char,Array of Byte 之间的互相转换
主要介绍了Javascript实现Array和String互转换的方法,涉及JavaScript中toString方法与split方法的使用技巧,需要的朋友可以参考下
xml转json,或xml转array xml转json,或xml转array
C# Array和ArrayList,List区别
10 Special Array Antenna Configurations 269 10.1 Conformal Array and Phased Array Antennas 269 10.1.1 Circular Sector Array and Phased Array Antenna 270 10.2 Volume Array and Phased Array Antennas 276...
复制代码 代码如下://PHP stdClass Object转array function object_array($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$...
A suffix array represents the suffixes of a string in sorted order. Being a simpler and more compact alternative to suffix trees, it is an important tool for full text indexing and other string ...
java List、Set与Array之间的相互转换 java List、Set与Array之间的相互转换
一款简单的翻译类app,代码中详细展示了array,adapter的使用,适合初学者学习参考
本工具类用于String、Array和String、Date之间的转化
javscript中json对象和Array数组的区别
本文详细讲解了java中Collection与Collections,Array与Arrays的区别。
Array Leet Code solved samples in Java
该资源主要介绍了android数组资源string-array integer-array的用法 包括int类型、string类型、文件类型的使用
1.1 Array Background 1 1.2 Systems Factors 2 1.3 Annotated Reference Sources 3 1.3.1 Adaptive Antenna Reference Books 5 References 5 2 Basic Array Characteristics 7 2.1 Uniformly Excited Linear Arrays...
缓冲区到 uint8array 将缓冲区(或字符串)转换为 Uint8Array例子 var tou8 = require ( 'buffer-to-uint8array' ) ;var buf = new Buffer ( 'whatever' ) ;var a = tou8 ( buf ) ;console . log ( a . constructor ...