今天认真看了下android适配器,学习了下它的使用方法。

  一,ArrayAdapter

  ArrayAdapter 比较简单,只可以存放一行文本信息。下面是简单的实现

    private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_adapter);
listView = (ListView)findViewById(R.id.listView3);
list = new ArrayList<String>();
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getData());
listView.setAdapter(adapter);
}
ArrayList<String> getData(){
list.add("第一行");
list.add("第二行");
list.add("第三行");
list.add("第四行");
return list;
}

  1,首先需要定义一个数组的集合,这里用ArrayList<String>类型,通过getData()动态的添加数据,

  2,定义一个ArrayAdapter,参数context   :只需要填入上下文this,第二个参数 resource:是一个布局文件,第三个参数List<T> objects:是一个数据集合。上面代码中的

android.R.layout.simple_list_item_1参数是一个系统自带的laout文件,通过按键ctrl+B查看源代码如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

  只是一个简单的TextView,垂直居中,当然如果不用系统自带的,用我们自己创建的也可以。

  3,将适配器和控件进行绑定。如果没有这一步,相当于我们创建的控件是不会显示出任何数据的。

二 SimpleAdapter

  SimpleAdapter 比ArrayAdapter稍微复杂一些,一般用在图片文字混排的数据结构中一个简单例子

 private SimpleAdapter adapter;
private ListView listView;
private List<Map<String,Object>> list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_adapter);
listView = (ListView)findViewById(R.id.listView2);
list = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(this,getData(),R.layout.itemlayout,new String[]{"pic","title","content"},new int[]{R.id.imageView,R.id.textView,R.id.textView2});
listView.setAdapter(adapter);
}
List<Map<String,Object>> getData(){
Map<String,Object> map = new HashMap<String, Object>();
map.put("pic",R.drawable.a);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.b);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.c);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.d);
map.put("title","标题");
map.put("content","内容");
list.add(map);
return list;
}

通过ctrl+B 查看SimpleAdapter 定义,其中参数

List<? extends Map<String, ?>> data是一个List的集合,成员是一个Map,
resource 同样是一个布局文件,只是这里的布局文件需要我们手动添加,
from     map中映射到控件ID的字符串  
to       就是我们定义的控件ID    
 public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {

这里的子布局文件使用自定义itemlayout

<?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">
<ImageView
android:layout_width="50dp"
android:layout_height="60dp"
android:id="@+id/imageView" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2" />
</LinearLayout>
</LinearLayout>

最后一点就是千万不能忘记setAdapter

android 适配器 ArrayAdapter,SimpleAdapter的学习的更多相关文章

  1. Android学习之适配器ArrayAdapter SimpleAdapter

    Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁.我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等. 常用的适配器 ...

  2. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  3. 13.Android-ListView使用、BaseAdapter/ArrayAdapter/SimpleAdapter适配器使用

    1.ListView ListView 是 Android 系统为我们提供的一种列表显示的一种控件,使用它可以用来显示我们常见的列表形式.继承自抽象类 AdapterView.继承图如下所示: 以微信 ...

  4. Android 常用数据适配器ArrayAdapter

    接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...

  5. android 71 ArrayAdapter和SimpleAdapter

    Activity和item: Activity:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  6. android 适配器simpleadapter和baseadapter区别

    android 适配器 simpleadapter 和 baseadapter 设计网络程序或者数据处理显示程序的时候,常常会使用 simpleadapter 和baseadapter 来实现. ad ...

  7. android适配器及监听点击和滚动在ListView中的使用

    package com.example.demon08; import java.util.ArrayList;import java.util.HashMap;import java.util.Li ...

  8. Android ListView ArrayAdapter 的简单使用

    前面写了3篇关于android的文章,其中的演示程序都写在了一个工程中,当时为了方便测试就在启动页MainActivity中放了3个按钮,点击不同的按钮进入不同的示例程序页面,MainActivity ...

  9. android之ArrayAdapter的重写

    昨天介绍了ArrayAdapter的使用,今天介绍一下更加实用的一点,对它进行重写,满足自己的个性化设计需要. ArrayAdapter(数组适配器)一般用于显示一行文本信息,所以比较容易. publ ...

随机推荐

  1. JS实现全选、不选、反选

    思路:1.获取元素.2.用for循环历遍数组,把checkbox的checked设置为true即实现全选,把checkbox的checked设置为false即实现不选.3.通过if判断,如果check ...

  2. Android --> 常见控件

    1.TextView  主要用于界面上显示一段文本信息 2.Button  用于和用户交互的一个按钮控件 //为Button点击事件注册一个监听器public class Click extends ...

  3. 初试spring-session

    一.简介 spring-session提供了用户会话信息管理的API和实现. 它将取代容器中的HttpSession.在没有容器会话集群方案的情况下,使得支持会话集群微不足道. 它支持在一个浏览器实例 ...

  4. C#深入学习 ----多线程学习(一)第一天学习

    学习最好的方法就是总结,并写下来,能让别人看懂,自己肯定是掌握了的. 针对软件开发,一直停留在能做的层次,今天得到大牛指点,觉得有必要对这门技术深入学习. 翻阅园内各大神的文章,收益匪浅,在这里做下总 ...

  5. ExtJs2.0里Ext.form.Radio水平排列布局

      ExtJs2.0好像不支持单选框组,因此用两个name相同单选框来实现单选框组 var radio1 = new Ext.form.Radio({boxLabel:'男',name:'sex',i ...

  6. 改变图像,运用match方法判断

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...

  7. Python 操作 MYSQL

    本文介绍了 Python 操作 MYSQL.执行 SQL 语句.获取结果集.遍历结果集.取得某个字 段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多, 是一桌丰盛唯美的代 ...

  8. 如何安装Orchard

    本篇文章主要讲解如何安装Orchard,首先说一下Orchard的安装方式有如下几种: 通过Microsoft WebMatrix(Microsoft Web Platform Installer)安 ...

  9. 一个简单的java贷款程序

    代码如下: //计算贷款package ClassDemo;import javax.swing.JOptionPane; public class ComputeLoan { public stat ...

  10. Android - Fragment (三)不同Fragment之间的通信

    在Fragment的java文件中,可以使用getActivity()来获得调用它的activity, 然后再找到另一个Fragment,进行通信 getActivity().getFragmentM ...