ArrayAdapter与SimpleAdapter的使用
在使用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的使用的更多相关文章
- 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理
在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
- ArrayAdapter、SimpleAdapter简单用法
1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects) context:上下 ...
- [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- android 71 ArrayAdapter和SimpleAdapter
Activity和item: Activity:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- android 适配器 ArrayAdapter,SimpleAdapter的学习
今天认真看了下android适配器,学习了下它的使用方法. 一,ArrayAdapter ArrayAdapter 比较简单,只可以存放一行文本信息.下面是简单的实现 private ListView ...
- Android -- ListView与ArrayAdapter、SimpleAdapter
对于ArrayAdapter,里面虽然能添加图片,但只能是相同的图片. 废话不多说: 布局&&list的item布局 ...
随机推荐
- Linux iptables
一.简介 http://liaoph.com/iptables/ 二.操作 1)查看规则 iptables -t filter -L -n iptables -t nat -L -n iptables ...
- Java中的静态方法和单例模式比较
区别 单例模式方法 静态方法 实例 创建实例 无 运行 类的实例的方法 类的方法 也可以通过实例化,在通过类的实例来运行 是否可以被重写 可以 可以(子类的该方法也必须是静态方法) 调用其他静态方法 ...
- linux—【用户和组的管理操作】(5)
用户:user 组:group 增加:add 修改:modify mod 删除:delete del useradd 增加用户 usermod 修改用户 userdel ...
- 使用EntityFramework6完成增删查改和事务
使用EntityFramework6完成增删查改和事务 上一节我们已经学习了如何使用EF连接数据库,并简单演示了一下如何使用EF6对数据库进行操作,这一节我来详细讲解一下. 使用EF对数据库进行操作, ...
- 如何解决python中urlopen超时问题
看代码: 利用urlopen中的超时参数设立一个循环 while True: try: page = urllib.request.urlopen(url, timeout=3) break exce ...
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...
- UVA 12532 Interval Product
线段树水题,忽略每个数的大小,只记住他们的正负即可,规规矩矩的代码.不过这是我第一次完全自己写的一次A的代码了.纪念一下... #include <iostream> #include & ...
- eclipse菜单解释及中英对照《二》
上篇文章主要介绍了eclipse中每个大的标题下的中英文及其用法. 感谢http://blog.csdn.net/li_jinjian2005/article/details/2831641这个博主. ...
- css3高级运动keyframes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Saltstack-进阶篇
查看minion端的文件内容 [root@linux-node2 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 1 ...