BaseAdapter就Android應用程式中經常用到的基礎資料配接器,它的主要用途是將一組資料傳到像ListView、Spinner、Gallery及GridView等UI顯示組件,它是繼承自介面類Adapter,1、Adapter類簡介1)、Adapter相關類結構如所示:自訂Adapter子類,就需要實現上面幾個方法,其中最重要的是getView()方法,它是將擷取資料後的View組件返回,如ListView中每一行裡的TextView、Gallery中的每個ImageView。 2)、Adapter在Android應用程式中起著非常重要的作用,應用也非常廣泛,它可看作是資料來源和UI組件之間的橋樑,其中Adapter、資料和UI之間的關係,可以用表示:3)、常用子類2、BaseAdapter簡介
BaseAdapter是實現了ListAdapter和SpinnerAdapter兩個介面,當然它也可以直接給ListView和Spinner等UI組件直接提供資料。相關類結構如所示:3、樣本樣本一:Gallery顯示一組圖片運行結果:
說明:上面一行圖片是Gallery畫廊,每次點擊一個Gallery圖片時,會同時在下面以大圖形式顯示出來該圖片布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:spacing="5px"
android:layout_height="wrap_content"
></Gallery>
<ImageView
android:id="@+id/iv"
android:layout_gravity="center_vertical"
android:layout_marginTop="20px"
android:layout_width="320px"
android:layout_height="320px"
></ImageView>
</LinearLayout>
MainActivity類:
package com.magc.adapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private Gallery gallery;
private ImageView imgview;
private int[] imgs = {R.drawable.a6,R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgview = (ImageView)findViewById(R.id.iv);
gallery = (Gallery)findViewById(R.id.gallery1);
MyImgAdapter adapter = new MyImgAdapter(this);
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(new OnItemClickListener() {
//使用者點擊圖片時,將該圖片的ResourceID設到下面的ImageView中去,
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
imgview.setImageResource(imgs[position]);
}
});
}
class MyImgAdapter extends BaseAdapter {
//自訂圖片Adapter以內部類形式存在於MainActivity中,方便訪問MainActivity中的各個變數,特別是imgs數組
private Context context;//用於接收傳遞過來的Context對象
public MyImgAdapter(Context context) {
super();
this.context = context;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
return imgs.length;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
return position;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return position;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//針對每一個資料(即每一個圖片ID)建立一個ImageView執行個體,
ImageView iv = new ImageView(context);//針對外面傳遞過來的Context變數,
iv.setImageResource(imgs[position]);
Log.i("magc", String.valueOf(imgs[position]));
iv.setLayoutParams(new Gallery.LayoutParams(80, 80));//設定Gallery中每一個圖片的大小為80*80。
iv.setScaleType(ImageView.ScaleType.FIT_XY);
return iv;
}
}
}
樣本2:通過一個提示框來選擇頭像的功能(Gallery和ImageSwitcher結合顯示圖片)待續……