适配器在 Android 中的作用是什么?

发布于 2024-09-18 12:47:02 字数 112 浏览 7 评论 0原文

我想知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

方圜几里 2024-09-25 12:47:02

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.

怂人 2024-09-25 12:47:02

假设您想要在 Android 应用程序中显示一个列表。
为此,您将使用 Android 提供的 ListView
ListView本身实际上并不包含任何数据。
它只是一个没有数据的 UI 元素。
您可以使用 Android 适配器填充 ListView

Adapter 是一个接口,其实现提供数据并控制该数据的显示。
ListView自己的适配器,完全控制ListView
展示。
因此适配器控制列表中显示的内容以及如何显示它。

Adapter 接口包含各种与 ListView 进行数据通信的方法。
您可以通过实现BaseAdapter从头开始创建自己的适配器。

public class ArrayAdapter<T> extends BaseAdapter implements Filterable {

// One of the constructors
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
    init(context, resource, textViewResourceId, Arrays.asList(objects));
}

void manyMoreMethods(){} 

}

让我们定义一个适配器:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   android.R.layout.simple_list_item_1, android.R.id.text1, values);
  • 第一个参数:Context
  • 第二个参数:行的布局
  • 第三个参数:写入数据的 TextView ID
  • 第四个参数:数据数组

Let’s assume you want to display a list in your Android app.
For this you will use the ListView provided by Android.
ListViews don’t actually contain any data themselves.
It’s just a UI element without data in it.
You can populate your ListViews by using an Android adapter.

Adapter is an interface whose implementations provide data and control the display of that data.
ListViews own adapters that completely control the ListView’s
display.
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 the ListView.
You can create your own adapter from scratch by implementing BaseAdapter.

public class ArrayAdapter<T> extends BaseAdapter implements Filterable {

// One of the constructors
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
    init(context, resource, textViewResourceId, Arrays.asList(objects));
}

void manyMoreMethods(){} 

}

Lets define an adapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   android.R.layout.simple_list_item_1, android.R.id.text1, values);
  • First parameter: Context
  • Second parameter: Layout for the row
  • Third parameter: ID of the TextView to which the data is written
  • Fourth parameter: The array of data
无人问我粥可暖 2024-09-25 12:47:02

它是数据源和布局(最有可能是 ListView)之间的接口。

打个比方

让我们以移动充电器或更确切地说 USB 电缆为例。
线材可以理解为适配器,而数据源和布局可以分别理解为插座(插入点)和USB端口(充电点)。

在手机充电的情况下,
电源的来源可能不同,
例如,通过移动电源、插座或笔记本电脑充电。
Android 中使用的适配器也是如此。
数据源可以根据应用要求而改变。

简而言之,Android 中的适配器携带来自源的数据
(例如ArrayList
并将其传递到布局(.xml 文件)。

It's an interface between the data source and your layout (most probably ListView).

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).

北凤男飞 2024-09-25 12:47:02

Android 中的适配器是适配器视图(例如ListView)和该视图的底层数据之间的桥梁。
想象一下如果没有适配器世界会是什么样子!

示例

  • 显示垂直滚动列表中的项目的视图。
    这些项目来自与此视图关联的 ListAdapter

  • ListAdapter 定义列表中各行的布局,
    通过 setAdapter() 方法向 ListView 提供数据
    ListView

  • Android提供了几种标准适配器;最重要的是
    ArrayAdapterCursorAdapter

  • 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 and
    provides data to the ListView via the setAdapter() method of
    ListView.

  • Android provides several standard adapters; the most important are
    ArrayAdapter and CursorAdapter.

  • ArrayAdapter can handle data based on arrays or lists.

  • SimpleCursorAdapter can handle database related data.
留一抹残留的笑 2024-09-25 12:47:02

适配器基本上用于传送内容。每个应用程序中可能都有的一个适配器是 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.

北城半夏 2024-09-25 12:47:02

适配器充当 AdapterView 和该视图的底层数据之间的桥梁。
适配器提供对数据项的访问,并负责为数据集中的每个项创建视图。

适配器是将View与某种数据源连接的智能方式。通常,您的视图将是 ListView,数据将以 CursorArray 的形式出现。
因此适配器是 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 a ListView and the data would come in form of a Cursor or Array.
So adapters come as subclasses of CursorAdapter or ArrayAdapter.

稳稳的幸福 2024-09-25 12:47:02

适配器管理数据模型并使其适应列表视图中的各个行。
它扩展了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.

南烟 2024-09-25 12:47:02

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.

稚气少女 2024-09-25 12:47:02

已经给出了多个答案,但我想给出不同的答案。

适配器意味着您可以它的桥提供者

适配器是一组数据和显示该数据的 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.

黑色毁心梦 2024-09-25 12:47:02

最后,适配器对于制作报告非常有用。如果想要显示某些信息的报告,可以使用此工具在视图上显示数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文