适配器在 Android 中的作用是什么? [英] What's the role of adapters in Android?

查看:85
本文介绍了适配器在 Android 中的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道何时哪里如何在Android环境中使用适配器.

I want to know when, where and how adapters 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 应用中显示一个列表.为此,您将使用 Android 提供的 ListView.ListView 本身实际上并不包含任何数据.它只是一个没有数据的 UI 元素.您可以使用 Android 适配器填充 ListView.

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 是一个接口,它的实现提供数据并控制数据的显示.ListView 自己的适配器,完全控制 ListView 的展示.所以适配器控制列表中显示的内容以及如何显示.

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.

Adapter 接口包括将数据传送到 ListView 的各种方法.您可以通过实现 BaseAdapter 从头开始​​创建自己的适配器.

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

  • 第一个参数:上下文
  • 第二个参数:行的布局
  • 第三个参数:写入数据的TextView的ID
  • 第四个参数:数据数组
  • 这篇关于适配器在 Android 中的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆