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

来源:互联网 发布:济南整站优化方案 编辑:程序博客网 时间:2024/06/05 11:23

上一篇文章我们介绍了关于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
packagecom.example.arrayadapter;
 
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
 
publicclass MainActivity extends Activity {
 
    privateString[] names = {"功能1","功能2","功能3","功能4","功能5","功能6"};
    privateListView lv;
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        //方法1  3参数构造函数,要求加载的layout文件只能有一个TextView
        lv.setAdapter(newArrayAdapter<String>(this, R.layout.simple_textview, names));
        //方法2  4参数构造函数,加载一个普通layout文件,并指定TextView的id
        lv.setAdapter(newArrayAdapter<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
<?xmlversion="1.0"encoding="utf-8"?>
<TextViewxmlns: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
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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
packagecom.example.simpleadapter;
 
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
 
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.renderscript.Int2;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;
 
publicclass MainActivity extends Activity {
 
    privateListView lv;
    privateList<Map<String, Object>> data;
    @Override
    protectedvoid 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(newSimpleAdapter(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
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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
原创粉丝点击