Android 开发笔记___基本适配器的使用__BaseAdapter

之前用到过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;
}
}
上一篇:转载 NPOI Excel 单元格背景颜色对照表


下一篇:JavaScript模板引擎artTemplate.js——两种方法实现性别的判定