之前用到过ArryAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据,但在实际应用中常常有更复杂的列表,比如同一项中存在多个控件,这时候用前面的两个会比较复杂,而且不易扩展。因此Android提供了适应性更强的BaseAdapter,该适配器允许开发者在别的代码文件中进行逻辑处理,大大提高了代码的可读性、可维护性。

从BaseAdapter派生的数据适配器主要实现下面三个方法:

  • 构造函数:指定适配器需要处理的数据集合。
  • getCount:获取数据项的个数。
  • getView:获取每项的展示视图,并对每项的内部空间进行业务处理

下面以spinner为载体演示如何操作BaseAdapter:

  一、编写列表项的布局文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitCenter" /> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" > <TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="left|center"
android:textColor="@color/black"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="left|center"
android:textColor="@color/black"
android:textSize="13sp" />
</LinearLayout> </LinearLayout>

  二、写个新的适配器继承BaseAdapter:

 package com.example.alimjan.hello_world.adapter;

 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; import com.example.alimjan.hello_world.bean.planet;
import com.example.alimjan.hello_world.R; import java.util.ArrayList; public class PlanetAdapter extends BaseAdapter implements OnItemClickListener,
OnItemLongClickListener { private LayoutInflater mInflater;
private Context mContext;
private int mLayoutId;
private ArrayList<planet> mPlanetList;
private int mBackground; public PlanetAdapter(Context context, int layout_id, ArrayList<planet> planet_list, int background) {
mInflater = LayoutInflater.from(context);
mContext = context;
mLayoutId = layout_id;
mPlanetList = planet_list;
mBackground = background;
} @Override
public int getCount() {
return mPlanetList.size();
} @Override
public Object getItem(int arg0) {
return mPlanetList.get(arg0);
} @Override
public long getItemId(int arg0) {
return arg0;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(mLayoutId, null);
holder.ll_item = (LinearLayout) convertView.findViewById(R.id.ll_item);
holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
planet planet = mPlanetList.get(position);
holder.ll_item.setBackgroundColor(mBackground);
holder.iv_icon.setImageResource(planet.image);
holder.tv_name.setText(planet.name);
holder.tv_desc.setText(planet.desc);
return convertView;
} public final class ViewHolder {
private LinearLayout ll_item;
public ImageView iv_icon;
public TextView tv_name;
public TextView tv_desc;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String desc = String.format("您点击了第%d个行星,它的名字是%s", position + 1,
mPlanetList.get(position).name);
Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show();
} @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String desc = String.format("您长按了第%d个行星,它的名字是%s", position + 1,
mPlanetList.get(position).name);
Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show();
return true;
}
}

  三、在页面代码中构造该适配器,,并应用于spinner对象:

 package com.example.alimjan.hello_world;

 import java.util.ArrayList;

 /**
* Created by alimjan on 7/16/2017.
*/ import com.example.alimjan.hello_world.adapter.PlanetAdapter;
import com.example.alimjan.hello_world.bean.planet;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener; public class class_5_2_1 extends AppCompatActivity { private ArrayList<planet> planetList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_5_2_1);
initSpinner();
} private void initSpinner() {
planetList = planet.getDefaultList();
PlanetAdapter adapter = new PlanetAdapter(this, R.layout.item_list, planetList, Color.WHITE);
Spinner sp = (Spinner) findViewById(R.id.sp_planet);
sp.setPrompt("请选择行星");
sp.setAdapter(adapter);
sp.setSelection(0);
sp.setOnItemSelectedListener(new MySelectedListener());
} private class MySelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(class_5_2_1.this, "您选择的是"+planetList.get(arg2).name, Toast.LENGTH_LONG).show();
} public void onNothingSelected(AdapterView<?> arg0) {
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_5_2_1.class);
mContext.startActivity(intent);
} }

plenet 类在这

 package com.example.alimjan.hello_world.bean;

 import java.util.ArrayList;
import com.example.alimjan.hello_world.R; /**
* Created by alimjan on 7/16/2017.
*/ public class planet {
public int image;
public String name;
public String desc; public planet() {
this.image = 0;
this.name = "";
this.desc = "";
} public planet(int image, String name, String desc) {
this.image = image;
this.name = name;
this.desc = desc;
} private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
private static String[] descArray = {
"水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
"金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
"地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
"火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
"木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
"土星为太阳系八大行星之一,排行第六,体积仅次于木星"
};
public static ArrayList<planet> getDefaultList() {
ArrayList<planet> planetList = new ArrayList<planet>();
for (int i=0; i<iconArray.length; i++) {
planetList.add(new planet(iconArray[i], nameArray[i], descArray[i]));
}
return planetList;
}
}

Android 开发笔记___基本适配器的使用__BaseAdapter的更多相关文章

  1. Android 开发笔记___时间选择器---timePicker

    像datepicker一样,也有timepicker. 同样有timepickerdialog 所用到的方法还是一样,监听时间选择器的变化. package com.example.alimjan.h ...

  2. Android 开发笔记___实战项目:购物车

    购物车的应用很广泛,电商app基本上都有它的身影.由于它用到了多种存储方式,通过项目对数据的存储有更高层次的了解. 1.设计思路 首先看看购物车的外观.第一次进入时里面是空的,去购物页面加入购物车以后 ...

  3. Android 开发笔记___存储方式__共享参数__sharedprefences

    Android 的数据存储方式有四种,这次是[共享参数__sharedprefences] 听起来挺别扭的,平时看到的app里面,当用户删除了一些软件以后下次安装,发现原来的设置还在,这种情况就是把一 ...

  4. Android 开发笔记___登陆app

    package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import android.con ...

  5. Android 开发笔记___复选框__checkbox

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  6. Android 开发笔记___初级控件之实战__计算器

    功能简单,实现并不难,对于初学者可以总和了解初级控件的基本使用. 用到的知识点如下: 线性布局 LinearLayout:整体界面是从上往下的,因此需要垂直方向的linearlayout:下面每行四个 ...

  7. Android 开发笔记___图像按钮__imageButton

    IMAGEBUTTON 其实派生自image view,而不是派生自button.,image view拥有的属性和方法,image button 统统拥有,只是imagebutton有个默认的按钮外 ...

  8. Android 开发笔记___滚动视图__scroll view

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  9. Android 开发笔记___图像视图__简单截屏

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

随机推荐

  1. oracle pl/sql 存储过程

    存储过程用于执行特定的操作,当建立存储过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数,可以将执行部分的数据传递到 ...

  2. 51 nod 1624 取余最长路 思路:前缀和 + STL(set)二分查找

    题目: 写这题花了我一上午时间. 下面是本人(zhangjiuding)的思考过程: 首先想到的是三行,每一行一定要走到. 大概是这样一张图 每一行长度最少为1.即第一行(i -1) >= 1, ...

  3. UI自动化测试(二)浏览器操作及对元素的定位方法(xpath定位和css定位详解)

    Selenium下的Webdriver工具支持FireFox(geckodriver). IE(InternetExplorerDriver).Chrome(ChromeDriver). Opera( ...

  4. 【充分利用你的Azure】将Azure用作云计算平台(1)

    本文将围绕几个步骤来讲. 因为本人是MSP,微软送了150刀的额度给我随便使用.这篇文章是要讲将Azure用作云计算平台,对于我来说,我是做机器学习的,那么Azure就要有机器学习的平台. 本文的目的 ...

  5. 使用vs编译事件来动态发布配置文件

    我们知道开发有很多的环境,一般我们会分为开发环境,测试环境,生产环境.而我们使用的vs默认配置就两种:Debug和Release.当然vs支持通过配置管理器来添加,编辑及删除配置. 为此不同的环境和配 ...

  6. Spring Boot-------JPA——EntityManager构建通用DAO

    EntityManager EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满 ...

  7. nodejs+express+mongoose无法获取数据库数据问题解决

    通过mongoose与mongodb进行操作.而mongoose是通过model来创建mongodb中对应的collection的,这样你通过如下的代码: mongoose.model('User', ...

  8. maven私服 nexus2.x工作目录解读(翻译文档)

    安装nexus repository manager oss 或pro版本时,会创建两个目录:一个目录包含运行环境及应用,通常符号链接为nexus:一个目录包含所有的配置和数据,通常为sonatype ...

  9. SSH/HTTPS安全的本质都依赖于TLS/SSL

    1.SSH/HTTPS的安全本质是TLS/SSL. 2.1990年互联网上的网页主要是静态内容,作为信息发布,使用HTTP明文传输是可以的.不过,后来很多公司开始使用网页进行金融交易,例如:股票,于是 ...

  10. OpenGL ES2.0贴图

    1.定义传入着色器的顶点数据及索引 //传入结构体 typedef struct { ]; ]; } Vertex; //顶点数据 const Vertex Vertices[] = { {{, -, ...