适配器在 Android 中的作用是什么?
我想知道在 Android 环境中何时、何处以及如何使用适配器。
Android 开发者文档中的信息对我来说还不够,我想得到更详细的分析。
I want to know when, where and how adapters are used in the context of Android.
The information from Android's developer documentation was insufficient for me and I'd like to get a more detailed analysis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Android 中的适配器基本上是 UI 组件和数据源之间的桥梁,用于将数据填充到 UI 组件中。
例如,列表(UI 组件)通过使用列表适配器从数据源数组填充。
Well adapters in Android are basically a bridge between the UI components and the data source that fill data into the UI Component
For example, Lists (UI Component) get populated by using a list adapter, from a data source array.
假设您想要在 Android 应用程序中显示一个列表。
为此,您将使用 Android 提供的
ListView
。ListView
本身实际上并不包含任何数据。它只是一个没有数据的 UI 元素。
您可以使用 Android 适配器填充
ListView
。Adapter
是一个接口,其实现提供数据并控制该数据的显示。ListView
自己的适配器,完全控制ListView
的展示。
因此适配器控制列表中显示的内容以及如何显示它。
Adapter
接口包含各种与ListView
进行数据通信的方法。您可以通过实现
BaseAdapter
从头开始创建自己的适配器。让我们定义一个适配器:
TextView
IDLet’s assume you want to display a list in your Android app.
For this you will use the
ListView
provided by Android.ListView
s don’t actually contain any data themselves.It’s just a UI element without data in it.
You can populate your
ListView
s by using an Android adapter.Adapter
is an interface whose implementations provide data and control the display of that data.ListView
s own adapters that completely control theListView
’sdisplay.
So adapters control the content displayed in the list as well as how to display it.
The
Adapter
interface includes various methods to communicate data to theListView
.You can create your own adapter from scratch by implementing
BaseAdapter
.Lets define an adapter:
TextView
to which the data is written打个比方
让我们以移动充电器或更确切地说 USB 电缆为例。
线材可以理解为适配器,而数据源和布局可以分别理解为插座(插入点)和USB端口(充电点)。
在手机充电的情况下,
电源的来源可能不同,
例如,通过移动电源、插座或笔记本电脑充电。
Android 中使用的适配器也是如此。
数据源可以根据应用要求而改变。
简而言之,Android 中的适配器携带来自源的数据
(例如
ArrayList
)并将其传递到布局(.xml 文件)。
An analogy
Let's take the example of a mobile charger, or rather a USB cable.
The wire can be considered as the adapter, while the data source and layout can be understood as the socket (plug-in point) and USB port (charging point) respectively.
In the case of mobile charging,
the source of the power may be different,
e.g. charging from a power bank, a socket or a laptop.
The same is the case for the adapters used in Android.
The data source may be changed depending upon the application requirement.
In short, an adapter in Android carries the data from a source
(e.g.
ArrayList<>
)and delivers it to a layout (.xml file).
Android 中的适配器是适配器视图(例如
ListView
)和该视图的底层数据之间的桥梁。想象一下如果没有适配器世界会是什么样子!
示例
显示垂直滚动列表中的项目的视图。
这些项目来自与此视图关联的
ListAdapter
。ListAdapter
定义列表中各行的布局,通过
setAdapter()
方法向ListView
提供数据ListView
。Android提供了几种标准适配器;最重要的是
ArrayAdapter
和CursorAdapter
。ArrayAdapter
可以处理基于数组或列表的数据。SimpleCursorAdapter
可以处理数据库相关数据。Adapters in Android are a bridge between the adapter view (e.g.
ListView
) and the underlying data for that view.Imagine what the world would have been without adapters!
Examples
A view that shows items in a vertically scrolling list.
The items come from the
ListAdapter
associated with this view.The
ListAdapter
defines the layout for individual rows of the list andprovides data to the
ListView
via thesetAdapter()
method ofListView
.Android provides several standard adapters; the most important are
ArrayAdapter
andCursorAdapter
.ArrayAdapter
can handle data based on arrays or lists.SimpleCursorAdapter
can handle database related data.适配器基本上用于传送内容。每个应用程序中可能都有的一个适配器是 CursorAdapter,它使您能够传递数据库查询中游标给出的内容。 ListView 几乎总是有某种适配器。
Adapters are basically used to deliver content. One adapter you probably have in every application is the CursorAdapter which enables you to deliver content given by a cursor from a database query. A ListView has nearly always some sort of Adapter.
适配器充当 AdapterView 和该视图的底层数据之间的桥梁。
适配器提供对数据项的访问,并负责为数据集中的每个项创建视图。
适配器是将
View
与某种数据源连接的智能方式。通常,您的视图将是ListView
,数据将以Cursor
或Array
的形式出现。因此适配器是 CursorAdapter 或 ArrayAdapter 的子类。
An adapter acts as a bridge between an
AdapterView
and the underlying data for that view.The adapter provides access to the data items and is responsible for creating a view for each item in the data set.
Adapters are a smart way to connect a
View
with some kind of data source. Typically, your view would be aListView
and the data would come in form of aCursor
orArray
.So adapters come as subclasses of
CursorAdapter
orArrayAdapter
.适配器管理数据模型并使其适应列表视图中的各个行。
它扩展了
BaseAdapter
类。列表视图中的每一行都包含一个布局,其复杂程度可以根据您的需要而定。
列表视图中的典型行在左侧有一个图像,在中间有两个文本行。
An adapter manages the data model and adapts it to the individual rows in the list view.
It extends the
BaseAdapter
class.Every line in the list view consists of a layout which can be as complex as you want.
A typical line in a list view has an image on the left side and two text lines in the middle.
Adapter只是用来实现listview的概念。不仅用于显示数据列表,还用于显示一些自定义视图。假设客户想要使用具有更多数量的文本视图(任何其他视图)的列表,那么我们必须在 Android 中使用适配器视图。
Adapter is simply used to achieve the listview concept. Not only for displaying the list of data but also the some custom view. Suppose customer wants to use the list which has more number of textview (any other view) then , we have to use Adapter view in Android.
已经给出了多个答案,但我想给出不同的答案。
适配器意味着您可以它的桥提供者。
适配器是一组数据和显示该数据的 AdapterView 之间的链接。
There are already given multiple answer,But I want to give a different answer.
Adapter means you can that Its bridge provider.
Adapters are the link between a set of data and the AdapterView that displays the data.
最后,适配器对于制作报告非常有用。如果想要显示某些信息的报告,可以使用此工具在视图上显示数据。
At the end, adapters are very useful to do a report. If one wants to show a report of some information, one can use this tool to show the data on the view.