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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3674951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:18:09  来源:igfitidea点击:

What's the role of adapters in Android?

androidadapterandroid-adapter

提问by Robin

I want to know when, whereand howadapters are used in the context of Android.

我想知道Android 上下文中何时何地以及如何使用适配器。

The information from Android's developer documentation was insufficient for me and I'd like to get a more detailed analysis.

Android 开发人员文档中的信息对我来说不够,我想获得更详细的分析。

采纳答案by Prashant_M

Let's assume you want to display a list in your Android app. For this you will use the ListViewprovided 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.

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

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

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

The Adapterinterface includes various methods to communicate data to the ListView. You can create your own adapter from scratch by implementing BaseAdapter.

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(){} 

}

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 TextViewto which the data is written
  • Fourth parameter: The array of data
  • 第一个参数:上下文
  • 第二个参数:行的布局
  • 第三个参数:TextView写入数据的ID
  • 第四个参数:数据数组

回答by success_anil

Well adapters in Android are basically a bridge between the UI components and the data source that fill data into the UI Component

Android 中的 Well 适配器基本上是 UI 组件和将数据填充到 UI 组件中的数据源之间的桥梁

For example, Lists (UI Component) get populated by using a list adapter, from a data source array.

例如,使用列表适配器从数据源数组填充列表(UI 组件)。

回答by Akhilesh Sinha

I would like to share my understanding.

我想分享一下我的理解。

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

它是数据源和布局(最有可能是 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.

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

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.

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

In short, an adapter in Android carries the data from a source (e.g. ArrayList<>) and delivers it to a layout (.xml file).

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

回答by Steve

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!

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

Examples

例子

  • A view that shows items in a vertically scrolling list. The items come from the ListAdapterassociated with this view.

  • The ListAdapterdefines the layout for individual rows of the list and provides data to the ListViewvia the setAdapter()method of ListView.

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

  • ArrayAdaptercan handle data based on arrays or lists.

  • SimpleCursorAdaptercan handle database related data.
  • 在垂直滚动列表中显示项目的视图。项目来自ListAdapter与此视图相关联的。

  • ListAdapter定义了列表中的各个行的布局和将数据提供给ListView经由所述setAdapter()的方法 ListView

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

  • ArrayAdapter可以处理基于数组或列表的数据。

  • SimpleCursorAdapter可以处理数据库相关的数据。

回答by Jainendra

An adapter acts as a bridge between an AdapterViewand 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.

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

Adapters are a smart way to connect a Viewwith some kind of data source. Typically, your view would be a ListViewand the data would come in form of a Cursoror Array. So adapters come as subclasses of CursorAdapteror ArrayAdapter.

适配器是一种将 aView与某种数据源连接起来的智能方式。通常,您的视图将是 aListView并且数据将以 aCursor或 的形式出现Array。所以适配器作为CursorAdapteror 的子类出现ArrayAdapter

回答by WarrenFaith

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.

适配器基本上用于传送内容。您可能在每个应用程序中都有一个适配器是 CursorAdapter,它使您能够从数据库查询中传递由游标给出的内容。ListView 几乎总是有某种适配器。

回答by kamal patidar

An adapter manages the data model and adapts it to the individual rows in the list view. It extends the BaseAdapterclass.

适配器管理数据模型并使其适应列表视图中的各个行。它扩展了BaseAdapter类。

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.

列表视图中的每一行都包含一个布局,可以根据需要进行复杂的布局。列表视图中的典型行在左侧有一个图像,在中间有两个文本行。

回答by Hemalatha M.R.

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.

Adapter 只是用来实现 listview 的概念。不仅用于显示数据列表,还用于一些自定义视图。假设客户想要使用具有更多文本视图(任何其他视图)的列表,那么我们必须在 Android 中使用适配器视图。

回答by Ramesh

There are already given multiple answer,But I want to give a different answer.

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

Adapter means you can that Its bridge provider.

Adapter 意味着你可以认为它的桥接提供者

Adapters are the link between a set of data and the AdapterView that displays the data.

适配器是一组数据和显示数据的 AdapterView 之间的链接。

回答by RobertoFRey

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.

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