Android开发之Adapter
学习android时,对于我这种初学者来说,刚开始接触控件,发现有的控件需要adapter有些不需要,对此我感到不解。所以决定一探究竟。
其实android是一个完全遵从MVC模式的框架,activity是C,Layout是V,Adapter是M,若没有Adapter,我们添加的数据大都是“死数据”。常用的Adapter有如下几种:
- BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
- ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
- SimpleAdapter有最好的扩充性,可以自定义出各种效果。
- SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。
2.应用案例
1)ArrayAdapter
列表的显示需要三个元素:
a.ListVeiw 用来展示列表的View。
b.适配器 用来把数据映射到ListView上的中介。
c.数据 具体的将被映射的字符串,图片,或者基本组件。
package com.example.list_view; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { ListView list1,list2=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1=(ListView)findViewById(R.id.list1);
list2=(ListView)findViewById(R.id.list2);
String []arr1={"孙悟空","牛魔王","铁扇公主"};
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,R.layout.array_item,arr1);
list1.setAdapter(adapter1); String []arr2={"java","c#","android"};
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,R.layout.array_item,arr2);
list2.setAdapter(adapter2);
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="10px"
android:shadowRadius="2"
android:shadowDx="4"
android:shadowDy="4"
android:shadowColor="#f0f"/>
上面代码使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件R.layout.array_item
数据源(一个字符串数组)。同时用setAdapter()完成适配的最后工作。
数据源也可以是List集合。
android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字。与上面我自定义的R.layout.array_item一样的道理。自定义的可以控制颜色字体等等。
同理:
SimpleAdapter
SimpleAdapter simplead = new SimpleAdapter(第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要 ,
第二个参数表示生成一个Map(String ,Object)列表选项 ,
第三个参数表示界面布局的id 表示该文件作为列表项的组件,
第四个参数表示该Map对象的哪些key对应value来生成列表项,
第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系 );
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" > <ListView
android:id="@+id/lt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
<?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/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f0f"
android:paddingLeft="10dp"/> <TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:paddingLeft="10dp"/> </LinearLayout> </LinearLayout>
package com.example.simpleadptertest; 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.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { private String[] name = { "剑萧舞蝶", "张三", "hello", "诗情画意" }; private String[] desc = { "魔域玩家", "百家执行", "高级的富一代", "妹子请过来..一个善于跑妹子的。。" }; private int[] imageids = { R.drawable.libai, R.drawable.nongyu,
R.drawable.qingzhao, R.drawable.tiger }; private ListView lt1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < name.length; i++) {
Map<String, Object> listem = new HashMap<String, Object>();
listem.put("head", imageids[i]);
listem.put("name", name[i]);
listem.put("desc", desc[i]);
listems.add(listem);
} /*SimpleAdapter的参数说明
* 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
* 第二个参数表示生成一个Map(String ,Object)列表选项
* 第三个参数表示界面布局的id 表示该文件作为列表项的组件
* 第四个参数表示该Map对象的哪些key对应value来生成列表项
* 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
* 注意的是map对象可以key可以找不到 但组件的必须要有资源填充 因为 找不到key也会返回null 其实就相当于给了一个null资源
* 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
* 这个head的组件会被name资源覆盖
* */
SimpleAdapter simplead = new SimpleAdapter(this, listems,
R.layout.simple_item, new String[] { "name", "head", "desc" },
new int[] {R.id.name,R.id.head,R.id.desc}); lt1=(ListView)findViewById(R.id.lt1);
lt1.setAdapter(simplead); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
SimpleCursorAdapter暂未学习如何在android中使用数据库。
Android开发之Adapter的更多相关文章
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之Java集合类性能分析
对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之Git配置
Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- Android开发之Java必备基础
Android开发之Java必备基础 Java类型系统 Java语言基础数据类型有两种:对象和基本类型(Primitives).Java通过强制使用静态类型来确保类型安全,要求每个变量在使用之前必须先 ...
- Android开发之PopupWindow
/* * Android开发之PopupWindow * * Created on: 2011-8-8 * Author: blueeagle * Email: liujiaxiang@g ...
- [置顶] Android开发之MediaPlayerService服务详解(一)
前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...
随机推荐
- spring-- 事务--9
9.1 数据库事务概述 事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务. 事务必需满足ACID(原子性.一致性.隔离性和持久性 ...
- 多组 RadioButtonList 获取值
<div class="row"> <table> <thead><tr><th>操作</th ...
- (5)I2C总线的10bit地址以及通用广播地址
其实,10bit地址我没用过,通用广播地址更没用过.通用广播地址应该是在多个mcu之间用i2c进行通信时使用的.虽说没用到,但还是做了翻译,说不定以后有机会用到: 10bit地址 10bit的寻址扩展 ...
- 微软Azure 存储管理器的简单介绍
Windows Azure存储用户经常希望能够在“管理器”中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我们将对此列表进行更新,使其 ...
- 终端ls显示的配色方案
打开~/.profile或者mac上的~/.bash_profile,加入: export CLICOLOR=1 export LSCOLORS=cxdxfxexbxegedabagacad 这是我的 ...
- 配置 Web Deploy 的步骤 -摘自网络
今天的文章里,我会介绍Microsoft Web Deploy—一个采用全面的发布和部署机制的免费服务器技术.Web Deploy不仅仅让你发布文件—还可以部署数据库结构/数据,运行变更的数据库脚本, ...
- [MySQL]快速解决"is marked as crashed and should be repaired"故障
具体报错如下: Table '.\Tablename\posts' is marked as crashed and should be repaired 提示说论坛的帖子表posts被标记有问题,需 ...
- Emmet:一个Html/Css快速编辑神器的插件
一.介绍:Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度 二.使用 ...
- html的框架
- Edit显示行号
Edit显示代码行号 关键点 使用这个类然后关联Edit的变量为 LineNumberEdit类型的 实现过程 //////////////////////////////////////////// ...