Android 适配器

本文介绍两个Adapter:

ArrayAdapter:最简单的Adapter,只能展现一行文字。

XML文件:

<ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent" >
</ListView>

代码:

ListView listView = findViewById(R.id.listView);
String str[] = new String[]{"点我开通会员","我的QQ钱包","我的个性装扮","我的收藏","相册"};
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_expandable_list_item_1,str);

效果图



BaseAdapter:最多使用的Adapter,实际开发中继承这个类重写相关方法。

XML文件:
主布局

<?xml version="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="horizontal"><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout>

数据布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/imageView"android:layout_width="90dp"android:layout_height="90dp"android:layout_marginTop="10dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginBottom="10dp"android:background="@drawable/ic_launcher_background"></ImageView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_gravity="center"><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="姓名"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="电话"android:textSize="20dp"></TextView></LinearLayout></LinearLayout>
</LinearLayout>

Java代码:
主类
因为用到图片,所以要在drawable文件夹里添加相应的图片资源

public class MainActivity extends AppCompatActivity {Context context = MainActivity.this;List list = new ArrayList();ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = findViewById(R.id.listView);list.add(new Data(R.drawable.tx,"小明","19656565656"));list.add(new Data(R.drawable.tx,"小红","18878787878"));baseAdapter adapter = new baseAdapter(context,list);listView.setAdapter(adapter);}
}

适配器

public class baseAdapter extends BaseAdapter {Context context;List <Data> list;public baseAdapter(Context context, List <Data> list){this.context=context;this.list=list;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int i) {return list.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {View v = LayoutInflater.from(context).inflate(R.layout.mydata, null);TextView textView1 = v.findViewById(R.id.textView1);TextView textView2 = v.findViewById(R.id.textView2);ImageView imageView = v.findViewById(R.id.imageView);imageView.setImageResource(list.get(i).tradmark);textView1.setText(list.get(i).name);textView2.setText(list.get(i).phone);return v;}
}

数据类

public class Data {int tradmark;String name;String phone;public Data(int trademark, String name,String phone){this.tradmark = trademark;this.name = name;this.phone = phone;}
}

效果图

Through love comes calm, and through calm comes thought.
爱使人平静,平静使人思考。

Android - Adapter 适配器相关推荐

  1. Android Adapter适配器

    Android Adapter适配器 Adapter的作用就是ListView界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View. Andr ...

  2. 纵观Android Adapter适配器。比较三种常用Adapter(SimpleAdapter,ArrayAdapter,BaseAdapter)。

    什么是适配器. 适配器在代码中体现为Adapter类.是连接数据和UI的桥梁. 换句话说将数据 配置 到你想配置的UI上.顾意思名:适配器 至于这个桥梁怎么制造,就看它的缔造者了. 为什么要使用适配器 ...

  3. android 之UI 高级控件Adapter(适配器详解)

    了解适配器组件: 适配器是一个连接数据源和AdapterView的桥梁,通过它能有效的实现数据源与AdapterView的分离设置,使AdaptereView与数据的绑定更加简便,修改更加方便. Ad ...

  4. Android中使用Adapter(适配器)给RecycleView设置数据源

    场景 RecyclerView RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足.其可以实现数据纵向滚动,也 ...

  5. android中Adapter适配器的讲解

    android中Adapter适配器的讲解 Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适 ...

  6. Android适配器以及作用,Android Studio:自定义Adapter(适配器)的一些通俗易懂的理解(以一个简单的聊天界面为例)...

    本文是博主对Adapter(适配器)的一些理解,为了加深对Adapter的理解以及记录自己的阶段学习而写,同时也适合初学者阅读,参考本条博客的逻辑进行学习. 第一  先来看看实现这个程序需要需要创建哪 ...

  7. Android进阶笔记09:Android 万能适配器

    1. Android 万能适配器      项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都 ...

  8. Adapter适配器和base-adapter-helper库的使用

    文章目录 Adapter适配器 概述 关于视图重用 关于ViewHolder 使用步骤 base-adapter-helper库的使用 1.导入库 2.将库中的java文件复制到项目中 3.编译并解决 ...

  9. Android Adapter接口的实现类总结

    一.Adapter的理解 (一)基本概念     首先,我们先要了解什么是Adapter,其实我们一般都可以从翻译中知道一点,这个单词的翻译是"适配器".也就是说,我们可以通过Ad ...

最新文章

  1. js日历代码_JS无形装逼,最为致命
  2. 利用条件GANs的pix2pix进化版:高分辨率图像合成和语义操作 | PaperDaily #23
  3. 在最美好的年华里,不要辜负最美的自己
  4. Django Rest Framework 视图和路由
  5. 【算法设计与分析】15 分治策略:芯片测试
  6. Android 系统(220)---如何快速对系统重启问题进行归类
  7. python调用ping命令_python调用系统命令ping
  8. 随想录(编译器是怎么工作的)
  9. E4/EAS/Eventing System 事件系统
  10. linux命令行使用
  11. Java最牛教材!甲骨文java认证考试试题
  12. 悬赏任务源码系统带app小程序源码基于php开源版
  13. 七日之都账号服务器,永远的7日之都开服时间一览表 7日之都最新服务器开服时间一览...
  14. java工具类-java仿微信九宫格头像
  15. 一个优秀开发者如何才能变得伟大?
  16. (简单)华为P20 EML-AL00的USB调试模式在哪里打开的步骤
  17. MPEG4与H.264的区别(音视频规范总结)
  18. AD 22 如何从SchDoc文件生成PcbDoc文件,进行布局和走线?
  19. c中将三个整数取平均值,结果保留两位小数
  20. 新基建缘何5G打头阵?

热门文章

  1. 江财计算机排名,江西财经大学排名
  2. android畅言作业平台,畅言作业平台学生端
  3. Web前端开发人员和设计师必读文章推荐【系列二】
  4. 22款奔驰GLC260L改装23P驾驶辅助系统,这几大功能你还是有必要知道的!
  5. 来自各大面经的一股清流 腾讯三面+华为三面【面试经验分享篇】
  6. 完美解决React 注册模块报错Missing message: “menu.xxx“ for locale: “zh-CN“, using default message as fallback问题
  7. 南邮汇编语言程序设计实验二————用户登录验证程序的设计
  8. 为iPhone 6设计自适应布局
  9. 程序人生 - 程序员学习新技术是要看书还是看视频?
  10. android 设置屏幕对比度,【Android】安卓调节屏幕亮度