一、适配器简介
- 适配器就是管理数据, 控件就是展示数据
- 数据的来源和子视图均由适配器控制
- 代码展示
1. SimpleCursorAdapter(Context context, int childLayout, Cursor c, String[] from, int[] to)
2. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[]{"Dave", "Satya", "Dylan"});
3. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
.array.planets, android.R.layout.simple_spinner_item);
二、适配器ArrayAdapter和微调框Spinner示例
1. 代码展示
视图xml:
<?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"
>
<!-- android:prompt列表选择的提示 -->
<Spinner android:id="@+id/spinner" android:prompt="@string/spinnerprompt"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
数据:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
public class SpinnerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
<!-- 第一个布局 -->
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets, android.R.layout.simple_spinner_item);
<!-- 选择之后,弹出的列表的布局 -->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
2. 效果图
二、自定义适配器和Gallery示例
1. 代码展示
主视图
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/gallery.xml -->
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
子视图
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#555"
android:scaleType="centerInside"
android:padding="5dip"
android:maxHeight="50dip"
android:maxWidth="50dip"
/>
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery)findViewById(R.id.gallery);
ManateeAdapter manateeAdapter = new ManateeAdapter(this);
gallery.setAdapter(manateeAdapter);
}
public static class ManateeAdapter extends BaseAdapter {
private static final String TAG = "ManateeAdapter";
private static int convertViewCounter = 0;
private Context mContext;
private LayoutInflater mInflater;
// 管理数据
static class ViewHolder {
ImageView image;
}
private int[] manatees = {
R.drawable.manatee00, R.drawable.manatee01, R.drawable.manatee02,
R.drawable.manatee03, R.drawable.manatee04, R.drawable.manatee05,
R.drawable.manatee06, R.drawable.manatee07, R.drawable.manatee08,
R.drawable.manatee09, R.drawable.manatee10, R.drawable.manatee11,
R.drawable.manatee12, R.drawable.manatee13, R.drawable.manatee14,
R.drawable.manatee15, R.drawable.manatee16, R.drawable.manatee17,
R.drawable.manatee18, R.drawable.manatee19, R.drawable.manatee20,
R.drawable.manatee21, R.drawable.manatee22, R.drawable.manatee23,
R.drawable.manatee24, R.drawable.manatee25, R.drawable.manatee26,
R.drawable.manatee27, R.drawable.manatee28, R.drawable.manatee29,
R.drawable.manatee30, R.drawable.manatee31, R.drawable.manatee32,
R.drawable.manatee33 };
private Bitmap[] manateeImages = new Bitmap[manatees.length];
private Bitmap[] manateeThumbs = new Bitmap[manatees.length];
public ManateeAdapter(Context context) {
Log.v(TAG, "Constructing ManateeAdapter");
this.mContext = context;
mInflater = LayoutInflater.from(context);
for(int i=0; i<manatees.length; i++) {
manateeImages[i] = BitmapFactory.decodeResource(
context.getResources(), manatees[i]);
// 创建数据的一个较小版本用于显示
manateeThumbs[i] = Bitmap.createScaledBitmap(manateeImages[i],
100, 100, false);
}
}
// 获取对象数量
public int getCount() {
Log.v(TAG, "in getCount()");
return manatees.length;
}
// 获取视图类型数量
public int getViewTypeCount() {
Log.v(TAG, "in getViewTypeCount()");
return 1;
}
// 获取视图类型
public int getItemViewType(int position) {
Log.v(TAG, "in getItemViewType() for position " + position);
return 0;
}
// 获取视图
// 参数convertView,用于缓存最近一个失去可视状态的列表项控件对象
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.v(TAG, "in getView for position " + position +
", convertView is " +
((convertView == null)?"null":"being recycled"));
if (convertView == null) {
// 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
// 2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素
convertView = mInflater.inflate(R.layout.gridimage, null);
convertViewCounter++;
Log.v(TAG, convertViewCounter + " convertViews have been created");
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.gridImageView);
// 对列表控件中的子控件进行缓存
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageBitmap(manateeImages[position]);
return convertView;
}
public Object getItem(int position) {
Log.v(TAG, "in getItem() for position " + position);
return manateeImages[position];
}
public long getItemId(int position) {
Log.v(TAG, "in getItemId() for position " + position);
return position;
}
}
}
2. 效果图