Android学习笔记(10)——数据适配器ArrayAdapter和SimpleAdapter

上一篇文章我们介绍了关于ListView的基本知识,也说到了一些关于数据适配器Adapter的问题,这里我们继续介绍两种比较常见的数据适配器的类型,ArrayAdapter和SimpleAdapter。这两种适配器各自有各自的特点,适用于不同的情况。

ArrayAdapter

这种适配器比较简单,常用于仅仅是文本内容的ListView、没有图标或者比较复杂的布局的情况下,实现方法主要就是去填写构造函数中的参数,ArrayAdapter函数的构造方法有好几种,这里介绍了最主要的两种情况,具体可以看代码中的注释。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.arrayadapter;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
public class MainActivity extends Activity {
 
     private String[] names = { "功能1" , "功能2" , "功能3" , "功能4" , "功能5" , "功能6" };
     private ListView lv;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         lv = (ListView) findViewById(R.id.lv);
         //方法1  3参数构造函数,要求加载的layout文件只能有一个TextView
         lv.setAdapter( new ArrayAdapter<String>( this , R.layout.simple_textview, names));
         //方法2  4参数构造函数,加载一个普通layout文件,并指定TextView的id
         lv.setAdapter( new ArrayAdapter<String>( this , R.layout.list_item, R.id.tv_item, names));
     }
 
}

MainActivity的xml文件很简单,就是一个LinearLayout加一个ListView,而simple_textview的xml文件如下:

?
1
2
3
4
5
6
7
8
<? xml version = "1.0" encoding = "utf-8" ?>
< TextView xmlns:android = "http://schemas.android.com/apk/res/android"
     android:id = "@+id/simple_tv"
     android:layout_width = "match_parent"
     android:layout_height = "wrap_content"
     android:textSize = "20sp" >
 
</ TextView >

list_item布局文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<? 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 = "vertical" >
 
     < TextView
         android:id = "@+id/tv_item"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:textSize = "18sp" />
 
</ LinearLayout >

两种方法其实差别不太大。

Android学习笔记(10)——数据适配器ArrayAdapter和SimpleAdapter-xge技术博客

SimpleAdapter

这种数据适配器虽然名字叫simple,用起来却要比另外两种要麻烦一些,它支持界面布局更加复杂的Listview显示,比如要实现左边一个icon ,右边一个文字说明这种情况,用SimpleAdapter来实现就比较简单了。为了实际演示效果我们可以去sdk里面找一些drawable-icon出来,放到drawable-hdpi文件里面。这里我随便找了几个:

Android学习笔记(10)——数据适配器ArrayAdapter和SimpleAdapter-xge技术博客

接下来我们来看一下关键的setAdapter一步怎么写,我们查看SimpleAdapter的函数介绍,发现这货构造函数有5个参数,分别是:

context 上下文,这个没什么好说的

data 官方解释是:A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in “from”简单的说就是一个List,每一个元素又是一个map集合,用来存放需要显示的数据对

resource 即数据要显示的布局文件的id

from 和 to  他们的功能是为了使data数据里面的key和布局文件中的id绑定起来,其中from里面是一个key的数组,而to 是一个int数组,表示布局文件中要显示key内容控件对应的R.id

这么一看,我们只需要细心的填写好这几个参数,API就会自动帮我们加载好界面了,我们来看看代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.example.simpleadapter;
 
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.renderscript.Int2;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
public class MainActivity extends Activity {
 
     private ListView lv;
     private List<Map<String, Object>> data;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         lv = (ListView) findViewById(R.id.lv);
         data = new ArrayList<Map<String,Object>>();
         
         Map<String,Object> map1 = new HashMap<String, Object>();
         map1.put( "nametext" , "我是第一个文本" );
         map1.put( "icon" , R.drawable.ic_menu_call); 
         
         Map<String,Object> map2 = new HashMap<String, Object>();
         map2.put( "nametext" , "我是第二个文本" );
         map2.put( "icon" , R.drawable.ic_menu_camera);   
         
         Map<String,Object> map3 = new HashMap<String, Object>();
         map3.put( "nametext" , "我是第三个文本" );
         map3.put( "icon" , R.drawable.ic_menu_chat_dashboard);
         
         data.add(map1);
         data.add(map2);
         data.add(map3);
 
         lv.setAdapter( new SimpleAdapter( this , data, R.layout.view_item, new String[] { "icon" , "nametext" }, new int [] {R.id.iv,R.id.tv}));
     }
}

其中view_item.xml里面是一个横向的ListView,有一个ImageView存放icon,以及一个TextView存放文字说明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? 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:gravity = "center_vertical"
     android:orientation = "horizontal" >
 
     < ImageView
         android:id = "@+id/iv"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
 
     < TextView
         android:id = "@+id/tv"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content" />
 
</ LinearLayout >

最后我们在模拟器上执行代码,得到最终效果,这样就比较美观了:

Android学习笔记(10)——数据适配器ArrayAdapter和SimpleAdapter-xge技术博客

通过SimpleAdapter,我们就可以设计出各式各样自定义风格的ListView了。

欢迎转载,请注明出处。



搬运自本人博客,xge技术博客

http://www.xgezhang.com/android_listview_intro.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值