Android中Adapter的一点一滴

一、Adapter接口及实现类

 二、Adapter分类:

1、ArrayAdapter:将数组或list集合的多个值包装成多个列表项;列表项只能是TextView

2、SimpleAdapter:将ist集合的多个对象包装成多个列表项;

3、SimpleCursorAdapter:包装Cursor提供的数据;

4、自定义Adapter:继承BaseAdapter,对列表项进行自定义;

三、代码

1.ArrayAdapter:


代码如下:

public class ArrayAdapterActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.arrayadapter);

		ListView list1 = (ListView) findViewById(R.id.list1);
		ListView list2 = (ListView) findViewById(R.id.list2);

		String[] array = { "张三", "李四", "王二" };
		/**
		 * 三参数型;
		 * Context context:上下文对象
		 * int resource:layout 确定在那个布局资源中显示 
		 * T[] objects :显示的数据
		 */
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				ArrayAdapterActivity.this, R.layout.simple_list_item1, array);
		list1.setAdapter(adapter);

		String[] array2 = { "Java", "Hibernate", "Spring", "Android" };
		/**
		 * 四参数型;
		 * Context context:上下文对象
		 * int resource:layout 确定在那个布局资源中显示 
		 *  int textViewResourceId :在布局中的id
		 * T[] objects :显示的数据
		 */
		ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
				ArrayAdapterActivity.this, R.layout.simple_list_item2,R.id.englishTv, array2);
		list2.setAdapter(adapter2);
	}
}
ListView的每一项的布局为:  simple_list_item1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:shadowColor="#f0f"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="2"
    android:textSize="24sp" />
simple_list_item2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/englishTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:shadowColor="#f0f"
        android:shadowDx="4"
        android:shadowDy="4"
        android:shadowRadius="2"
        android:textSize="24sp"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" />

</LinearLayout>


public class SimpleAdapterActivity extends Activity {
	private ListView myLv;
	private SimpleAdapter adapter;
	private String[] names = { "中国", "美国", "英国", "日本" };
	private String[] desc = { "CHINA ", "AMERICA", "ENGLAND", "JAPAN" };
	private int[] imageId = { R.drawable.b3, R.drawable.b4, R.drawable.b5,
			R.drawable.b6 };
	private List<Map<String, Object>> allList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simpleadapter);

		myLv = (ListView) findViewById(R.id.countryLv);
		/**
		 * 第一步准备 V :view 视图 第二步准备 M :model 数据 第三步准备 c :controller
		 * 控制器(这里是adapter) context, data, resource, from, to
		 */
		allList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < names.length; i++) {
			Map<String, Object> oneItem = new HashMap<String, Object>();
			oneItem.put("flag", imageId[i]);
			oneItem.put("name", names[i]);
			oneItem.put("desc", desc[i]);
			allList.add(oneItem);
		}
		/**
		 * 控制器(这里是adapter) context:getApplicationContext() data: allList
		 * resource :R.layout.simple_list_item3 from: to:
		 */
		adapter = new SimpleAdapter(getApplicationContext(), allList,
				R.layout.simple_list_item3, new String[] { "flag", "name",
						"desc" }, new int[] { R.id.countryImage, R.id.nameTv,
						R.id.disTv });
		myLv.setAdapter(adapter);
	}
}
simple_list_item3.xml为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/countryImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/nameTv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:textColor="#f00"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/disTv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:textColor="#0f0"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>


3、SimpleCursorAdapter :包装 Cursor 提供的数据;
public class SimpleCursorAdapterActivity extends Activity {
	private ListView musicLv;
//	private List<Map<String, Object>> musicList;
	private SimpleCursorAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simplecursoradapter);
		// 第一步:View
		musicLv = (ListView) findViewById(R.id.musicLv);
		// 第二步 : data
//		musicList = new ArrayList<Map<String, Object>>();
		Cursor mCursor = getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.AudioColumns._ID,
						MediaStore.Audio.AudioColumns.DISPLAY_NAME,
						MediaStore.Audio.AudioColumns.DATA,
						MediaStore.Audio.AudioColumns.ARTIST }, null, null,
				MediaStore.Audio.AudioColumns.DISPLAY_NAME + "  asc");
		// while (mCursor.moveToNext()) {
		// Map<String, Object> item = new HashMap<String, Object>();
		// item.put(
		// "musicName",
		// mCursor.getString(mCursor
		// .getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DISPLAY_NAME)));
		// item.put("who", mCursor.getString(mCursor
		// .getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.ARTIST)));
		// musicList.add(item);
		// }
		// 第三步:controller :adapter
		/**
		 * context: layout: c: from: to: flags:
		 */
		adapter = new SimpleCursorAdapter(getApplicationContext(),
				R.layout.simple_musiclist_item3, mCursor, new String[] {
						MediaStore.Audio.AudioColumns.DISPLAY_NAME,
						MediaStore.Audio.AudioColumns.ARTIST }, new int[] {
						R.id.musicnameTv, R.id.whoTv },
				SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

		musicLv.setAdapter(adapter);
	}
simple_musiclist_item.xml布局代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/musicImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/musicnameTv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:textColor="#f00"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/whoTv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:textColor="#0f0"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值