在使用ListView中我们使用到adapter,android中为我们不仅提供了BaseAdapter类来让我们自定义自己的Adapter,还为我们提供了ArrayAdapter以及SimpleAdapter。现在让我们简述一下,这两个类的使用方法。

package xidian.dy.com.chujia;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.lv);
String[] str = new String[]{"fdfas", "fdsf", "fafda"};
if(lv != null)
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_list, R.id.dog, str));
}
}

ArrayAdapter第一个参数是上下文,一般都是this来指定当前的activity,第二个参数是子布局文件,将来要放到ListView中,第三个是子布局文件中需要填充的对象ID,第四个是填充的值,类型为数组。先看一下子布局文件。

  • item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context="xidian.dy.com.chujia.MainActivity"> <ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/dog"
/>
<TextView
android:id="@+id/dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小狗"
android:textSize="30sp"
android:layout_gravity="center_vertical"
/>
</LinearLayout>

R.id.dog就是子布局文件中的TextView的id,ArrayAdapter没生成一个子布局对象后,会从str中取一个值出来,将其填充到TextView中。然后将生成的View对象交给ListView来显示。ArrayAdapter只能处理一种数据类型,这里的泛型指定的是String类,所以其第四个参数只能传入String数组。

SimpleAdapter


SimpleAdaptern能够处理多种数据,其用法和ArrayAdapter很相似。

package xidian.dy.com.chujia;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.lv);
List<Map<String, Object>> data = new ArrayList<>();
Map<String, Object> m1 = new HashMap<>();
m1.put("phone", R.drawable.dog);
m1.put("name", "one");
data.add(m1); Map<String, Object> m2 = new HashMap<>();
m2.put("phone", R.drawable.dog1);
m2.put("name", "two");
data.add(m2); Map<String, Object> m3 = new HashMap<>();
m3.put("phone", R.drawable.dog2);
m3.put("name", "three");
data.add(m3);
if(lv != null)
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_list, new String[]{"phone", "name"}, new int[]{R.id.phone, R.id.name}));
}
}

布局文件中有一个ImageView和一个TextView,我们一张图和一个字符串放到一个Map中,然后将多个Map放到一个List中。将List传递给SimpleAdapter。第三个参数和第四个参数的含义是,从Map中取出phone放到布局中的R.id.phone中,将name放到R.id.name中。

ArrayAdapter与SimpleAdapter的使用的更多相关文章

  1. 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理

    在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...

  2. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

  3. ArrayAdapter、SimpleAdapter简单用法

    1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects)   context:上下 ...

  4. [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段

      收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...

  5. 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter

    Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...

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

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

  7. android 71 ArrayAdapter和SimpleAdapter

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

  8. android 适配器 ArrayAdapter,SimpleAdapter的学习

    今天认真看了下android适配器,学习了下它的使用方法. 一,ArrayAdapter ArrayAdapter 比较简单,只可以存放一行文本信息.下面是简单的实现 private ListView ...

  9. Android -- ListView与ArrayAdapter、SimpleAdapter

    对于ArrayAdapter,里面虽然能添加图片,但只能是相同的图片. 废话不多说: 布局&&list的item布局                                   ...

随机推荐

  1. esxi安装全过程及基本配置

    esxi6.0下载地址 链接: http://pan.baidu.com/s/1jIfg2yU 密码: qacv 支持检测可以参考:http://www.linuxidc.com/Linux/2012 ...

  2. tfs witadmin

    有时候对TFS的操作需要使用命令行,因为无图形界面进行操作. 我们可以进入Visual Studio Tools使用Developer Command Prompt进行操作. 使用命令 witadmi ...

  3. [转]artDialog

    本文转自:http://aui.github.io/artDialog/ http://aui.github.io/artDialog/doc/index.html artDialog —— 经典的网 ...

  4. 边工作边刷题:70天一遍leetcode: day 84

    Flatten 2D Vector 要点: 这题是2d的iterator,一般对于1d的情况,hasNext()是不需要做移动的.而2d不同,core iterator是j向的,而i向要在hasNex ...

  5. 翻译《Writing Idiomatic Python》(二):函数、异常

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  6. UVALive 6663 Count the Regions --离散化+DFS染色

    题意:给你n(n<=50)个矩形(左上角坐标和右下角坐标),问这些矩形总共将平面分成多少个部分.坐标值可能有1e9. 分析:看到n和坐标的范围,容易想到离散化,当时就没想到离散化以后怎么判断区域 ...

  7. Android系列----JUnit单元测试的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  8. WPF窗口阴影和夜间模式的实现

    窗口阴影 实现 因项目需要给用户一定提示,设计师建议在鼠标进入时显示窗口阴影,离开时取消窗口阴影. 很自然,都会想到直接在窗口的内容或者自定义窗口的最外层元素上加效果.示例如下: <Grid&g ...

  9. [Unity2D]2D Mobile Joystick

    效果预览 操作步骤 1.下载素材 http://pan.bai du.com/s/1gdkQz8v 2.新建一个GUITexture(Joystick)及一个Sprite(Nyan)   3.添加背景 ...

  10. 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*

    2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...