在ArrayAdapter适配器和SimpleAdapter适配器以及ListView的使用一文中介绍了ListView和常用Adapter的使用,在本文中补充一种Adapter:SimpleCursorAdapter,虽然该Adapter不是很常用,但在有时候还是有用,下面简单介绍一下,代码如下:
Activity:
package com.home; import android.os.Bundle; import android.provider.Contacts.People; import android.support.v4.widget.SimpleCursorAdapter; import android.widget.ListAdapter; import android.app.ListActivity; import android.database.Cursor; public class TestSimpleCursorAdapterActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(cursor); // 实例化列表适配器 ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1, cursor, new String[] { People.NAME }, new int[] { android.R.id.text1 }); // 为Listview设置适配器 setListAdapter(adapter); } }
要访问系统联系人,别忘了加上下面权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>