Android——SimpleAdapter的简单使用(1)

在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

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.HashMap;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

  • Android前沿技术大纲

  • 全套体系化高级架构视频

Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、混合式开发(ReactNative+Weex)全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

DIy-1712208072081)]

Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、混合式开发(ReactNative+Weex)全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 28
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一种优雅的方式来使用RecyclerView核心思想  想必大家都遇到过,在一个列表中显示不同样式的需求。在RecyclerView中可以通过ViewType进行区分,如果样式特别多的时候就会使得代码非常冗余,不利于开发及维护。那么有没有一种优雅的方法解决这个问题呢?  技术经理给你说,接下来的项目由你负责,明天下班前把排期同步出来。这时你应该怎么做?由于你是Android端的RD,你对Android的排期是比较了解的,但是iOS端、FE端、Server端的排期怎么办呢?聪明的你立即把任务派发下去了,给每个端的负责人说,明天中午之前把排期汇总给我。  没错,在多样式列表的设计中也可以采用这种策略,给RecyclerView设置的Adapter不做具体的处理,而是由它派发出去。实现方案  1. addDelegate 向Adapter中注册委托Adapter;  2. addDataList 设置数据;  3. layout 渲染布局,Adapter查找到对应的委托Adapter,由委托Adapter去做具体渲染。如何使用引入compile 'com.kevin:delegationadapter:1.0.2' // 扩展库,使得databinding更简易,可以不引入 compile 'com.kevin:delegationadapter-extras:1.0.0'用法 1. 同一数据类型多种样式// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); // 设置Adapter delegationAdapter = new DelegationAdapter(); // 添加委托Adapter delegationAdapter.addDelegate(new OnePicAdapterDelegate()); delegationAdapter.addDelegate(new ThreePicAdapterDelegate()); delegationAdapter.addDelegate(new MorePicAdapterDelegate()); delegationAdapter.addDelegate(new VideoAdapterDelegate()); recyclerView.setAdapter(delegationAdapter);2. 不同数据类型多种样式// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); mDelegationAdapter.addDelegate(new StringAdapterDelegate()); mDelegationAdapter.addDelegate(new IntegerAdapterDelegate()); mDelegationAdapter.addDelegate(new FloatAdapterDelegate()); mDelegationAdapter.addDelegate(new DoubleAdapterDelegate()); // 添加委托Adapter mRecyclerView.setAdapter(mDelegationAdapter); // 设置数据 List<Object> dataList = new ArrayList<>(); dataList.add("今天天气怎么样?");  // 添加一个String类型数据 dataList.add("大晴天,有点热。");  // 添加一个String类型数据 dataList.add("温度多少度呢?");    // 添加一个String类型数据 dataList.add(29);                // 添加一个int类型数据 dataList.add("具体是多少?");      // 添加一个String类型数据 dataList.add(29.5F);             // 添加一个Float类型数据 dataList.add(29.527921364978D);  // 添加一个Double类型数据 mDelegationAdapter.setDataItems(dataList); 3. 同一数据多种类型// 设置LayoutManager mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 设置LayoutManager mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new ServiceInfoAdapterDelegate()); mDelegationAdapter.addDelegate(new BillItemAdapterDelegate()); mDelegationAdapter.addDelegate(new ChargeInfoAdapterDelegate()); mBinding.recyclerView.setAdapter(mDelegationAdapter); // 设置数据 String newsListStr = LocalFileUtils.getStringFormAsset(this, "bill.json"); Bill bill = new Gson().fromJson(newsListStr, Bill.class); List<Object> dataList = new ArrayList<>(); dataList.add(new ItemData(bill, ServiceInfoDelegateAdapter.TAG)); dataList.addAll(bill.details); dataList.add(new ItemData(bill, ChargeInfoDelegateAdapter.TAG)); mDelegationAdapter.setDataItems(dataList);4. 添加头部// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); mDelegationAdapter.addDelegate(new BannerAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter); // 添加头部 mDelegationAdapter.addHeaderItem("这是添加的添加的头部数据"); 5. 添加尾部// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); mDelegationAdapter.addDelegate(new BannerAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter); // 添加尾部 mDelegationAdapter.addFotterItem("这是添加的添加的尾部数据"); 6. 带兜底的委托Adapter// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); // 添加兜底的委托Adapter mDelegationAdapter.setFallbackDelegate(new FallbackAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter);THANKS TO 1. MultiItem 委托思想来源 2. AdapterDelegates 委托架子来源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值