只对简单应用进行描述。适配器与ListView配合使用可以快速生成item,效果如下例所示

一、简单模式

方式一

xml

    <ListView
android:id="@+id/lv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

java

package com.example.app03;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.LinkedList;
import java.util.List; public class MainActivity extends AppCompatActivity { private List<Animal> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //要显示的数据
String[] strs = {"AA","BB","CC","DD","EE","FF","GG","HH","II","JJ"};
//创建ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_single_choice ,strs);
//获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器
ListView list_test = (ListView) findViewById(R.id.lv_test);
list_test.setAdapter(adapter); }
}

效果

方式二

创建ListText(即显示的item信息):选中Values-右键new-xml-values xml file,如下图所示。

在文件中添加如下内容,其中name即为此数据文件的标识,定义好此文件后方式一中的adapter也可以这样定义(此方法不需要下述代码)

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.ListText,android.R.layout.simple_list_item_multiple_choice );
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ListText">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
</resources>

xml

 android:entries="@array/ListText" 比方法一增加的一行,默认数据在ListText中
    <ListView
android:id="@+id/lv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/ListText"
/>

直接运行即可如下效果

一、复杂模式

先来一个效果图

1)准备三张图片

2)新建一个布局,xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- 定义一个用于显示头像的ImageView -->
<ImageView
android:id="@+id/iv_head"
android:baselineAlignBottom="true"
android:paddingLeft="8dp"
android:layout_width="64dp"
android:layout_height="64dp" /> <!-- 定义一个竖直方向的LinearLayout,把QQ呢称与说说的文本框设置出来 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" /> </LinearLayout> </LinearLayout>

3)新建两个类:Animal动物类用来存储动物的图标,name和说说;AnimalAdapter类继承BaseAdapter

package com.example.app03;

public class Animal
{
private String aName;
private String aSpeak;
private int aIcon; public Animal() {
} public Animal(String aName, String aSpeak, int aIcon) {
this.aName = aName;
this.aSpeak = aSpeak;
this.aIcon = aIcon;
} public String getaName() {
return aName;
} public String getaSpeak() {
return aSpeak;
} public int getaIcon() {
return aIcon;
} public void setaName(String aName) {
this.aName = aName;
} public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
} public void setaIcon(int aIcon) {
this.aIcon = aIcon;
}
}
package com.example.app03;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import java.util.LinkedList; public class AnimalAdapter extends BaseAdapter
{
private LinkedList<Animal> mData;
private Context mContext; public AnimalAdapter(LinkedList<Animal> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
} @Override
public int getCount() {
return mData.size();
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.animal_list,parent,false);
ImageView img_icon = (ImageView) convertView.findViewById(R.id.iv_head);
TextView txt_aName = (TextView) convertView.findViewById(R.id.tv_name);
TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.tv_says);
img_icon.setBackgroundResource(mData.get(position).getaIcon());
txt_aName.setText(mData.get(position).getaName());
txt_aSpeak.setText(mData.get(position).getaSpeak());
return convertView;
}
}

4)MainActivity中代码

package com.example.app03;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.LinkedList;
import java.util.List; public class MainActivity extends AppCompatActivity { private List<Animal> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mContext = MainActivity.this;
list_animal = (ListView) findViewById(R.id.lv_test);
mData = new LinkedList<Animal>();
mData.add(new Animal("狗1", "我是狗1", R.drawable.aa));
mData.add(new Animal("狗2", "我是狗2", R.drawable.bb));
mData.add(new Animal("狗3", "我是狗3", R.drawable.cc));
mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
list_animal.setAdapter(mAdapter);
}
}

原文链接:http://www.runoob.com/w3cnote/android-tutorial-adapter.html

(四)适配器Adapter的更多相关文章

  1. 设计模式--适配器(Adapter)模式

    今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...

  2. 【原】模式之-适配器Adapter模式

    适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...

  3. Ruby设计模式透析之 —— 适配器(Adapter)

    转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9400153 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...

  4. 理解什么是适配器(adapter)和接口(interface)

    ● 适配器(adapter) In computing, adapter is a hardware device or software component that converts transm ...

  5. 设计模式学习心得<适配器 Adapter>

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接 ...

  6. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  7. 适配器(Adapter)模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...

  8. Android Studio 学习笔记(四):Adapter和RecyclerView说明

    在现版本中,滚动控件有多种,而相比于ListView,GridView,RecyclerView的用途更广,因此将前两者作为Adapter适配器的引入,再对RecyclerView进行简单讲解. MV ...

  9. 如何实现 axios 的自定义适配器 adapter

    Axios 是一个非常优秀的基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.并且提供了很多便捷的功能,例如: 支持 Promise API 拦截请求和响应 转换请求数据和 ...

随机推荐

  1. OpenGL在ubuntu下的成功配置

    sudo apt-get update sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev sudo a ...

  2. Java查询判断素数实验报告

    实验源代码: package sushu; import java.util.Scanner; public class First { int size=2; int data[]=new int[ ...

  3. Spring boot集成Rabbit MQ使用初体验

    Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...

  4. selenium自动化测试-浏览器基本操作

    webdriver 通过协议和接口发现DOM中的元素,并实现控制浏览器的行为,例如打开浏览器.控制浏览器大小. 浏览器刷新及浏览器前进.后退等,接下来介绍浏览器的一些基本操作. 1.启动浏览器 dri ...

  5. 设计模式-Builder和Factory模式区别

    Builder和Factory模式区别 Builder模式结构: Factory模式一进一出,Builder模式是分步流水线作业.当你需要做一系列有序的工作或者按照一定的逻辑来完成创建一个对象时 Bu ...

  6. Maven 梳理-手动创建Maven项目(非web),使用Maven编译、测试、打包、安装、引用

    创建空目录 F:\jtDevelop\maventest\myapp 创建pom.xml文件 <project xmlns="http://maven.apache.org/POM/4 ...

  7. Maven私服Nexus的搭建

    # Maven私服Nexus的搭建 ## 私服存在的合理性 Maven中的依赖是从服务器仓库中下载的,Maven的仓库只有两大类: - 1) 本地仓库 - 2) 远程仓库,其中在远程仓库中又分成了3种 ...

  8. Dubbo学习系列之十五(Seata分布式事务方案TCC模式)

    上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...

  9. ef core实现软删除

    很多web程序一般的偶不会设计真的物理删除了. 基本上都是在在数据库加一个标记,就得当作已经删除了.同时在查询的时候,过滤已经标记删除的数据 ef core实现软删除是非常简单的,直接在OnModel ...

  10. 04-10 Bagging和随机森林

    目录 Bagging算法和随机森林 一.Bagging算法和随机森林学习目标 二.Bagging算法原理回顾 三.Bagging算法流程 3.1 输入 3.2 输出 3.3 流程 四.随机森林详解 4 ...