当前位置:   article > 正文

Android——SimpleAdapter的简单使用,感悟分享_学习adapter 中的 simpleadapter 心得

学习adapter 中的 simpleadapter 心得

在这里插入图片描述

在Android中,Adapter(适配器)是数据与ui之间的桥梁,它把后台数据与前端ui连接到一起,是一个展示数据的载体。Adapter有很多的接口、抽象类、子类可以使用,以下是常用的几个适配器。

BaseAdapter:是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;

ArrayAdapter:支持泛型操作,最为简单,只能展示一行字。

SimpleAdapter:有最好的扩充性,可以自定义出各种效果。

SimpleCursorAdapter:可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

这里,主要介绍SimpleAdapter的简单用法

2.SimpleAdapter


构造方法:

SimpleAdapter(Context context, List<? extends Map<String,>>data,int resource,String[ ] from, int[ ] to)

参数:

1.context:上下文。

2.data:基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。

3.resource :就是一个布局layout,可引用系统提供的,也可以自定义。

4.from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。即 map 中得key值。

5.to:指定要填充的组件

3.SimpleAdapter实例——创建ListView


(1)main.xml

在布局文件中定义一个ListView

<?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=“wrap_content”

android:orientation=“horizontal”>

<ListView

android:id="@+id/mylist"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:divider="#000"

android:dividerHeight=“2dp”

android:listSelector="#600" />

(2)simpleadapter.xml

<?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=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id="@+id/text"

android:layout_width=“300dp”

android:layout_height=“wrap_content”

android:padding=“25dp”

android:textSize=“20sp”

android:textColor="#000" />

<ImageView

android:id="@+id/image"

android:layout_width=“80dp”

android:layout_height=“80dp”

android:padding=“10dp” />

*(3)MainActivity.java

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取
  • 1
  • 2
  • 3

package com.example.myapplication;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/83775
推荐阅读
相关标签
  

闽ICP备14008679号