【Android】--操作聯繫人的ContentProvider

1.main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="手機聯繫人列表"
        android:textSize="20px" />
 
    <ListView
        android:id="@+id/contactlists"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>

2.contacts.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TableRow>
 
        <TextView
            android:id="@+id/_id"
            android:layout_width="60px"
            android:layout_height="wrap_content"
            android:textSize="15px" />
 
        <TextView
            android:id="@+id/name"
            android:layout_width="300px"
            android:layout_height="wrap_content"
            android:textSize="15px" />
    </TableRow>
 
</TableLayout>

3…java代碼如下:

package org.lxh.demo;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
public class ContactsDemo extends Activity {
	private Cursor result = null ;	// 既然要查詢,查詢返回的就是結果
	private ListView contactsList = null ;	// 定義ListView組件
	private List<Map<String,Object>> allContacts = null ;
	private SimpleAdapter simple = null ;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.contactsList = (ListView) super.findViewById(R.id.contactsList) ;	// 取得組件
		this.result = super.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);	// 查詢
		super.startManagingCursor(this.result) ;	// 將結果集交給容器管理
		this.allContacts = new ArrayList<Map<String,Object>>() ;	// 實例化List集合
		for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result
				.moveToNext()) {	// 取出結果集中的每一個內容
			Map<String,Object> contact = new HashMap<String,Object>() ;
			contact.put("_id", this.result.getInt(this.result
					.getColumnIndex(ContactsContract.Contacts._ID)));
			contact.put("name", this.result.getString(this.result
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
			this.allContacts.add(contact) ;
		}
		this.simple = new SimpleAdapter(this, this.allContacts,
				R.layout.contacts, new String[] { "_id", "name" }, new int[] {
						R.id._id, R.id.name });
		this.contactsList.setAdapter(this.simple) ;
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章