【数据存储】SQLite数据库存储(10)----- 操作通讯记录的ContentProvider

【数据存储】SQLite数据库存储(10)----- 操作通讯记录的ContentProvider_第1张图片

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

 

<?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">
    <ListView 
        android:id="@+id/callList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
View Code

 

<uses-permission 
             android:name="android.permission.READ_CONTACTS" />
    <uses-permission 
             android:name="android.permission.WRITE_CONTACTS" />
View Code

 

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.CallLog;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MyCallContentProviderDemo extends Activity {
    // 定义ListView组件
    private ListView callList = null ;
    // 全部联系人
    private Cursor result = null ;
    // 用于设置SimpleAdapter
    private List<Map<String,Object>> allCalls = null ;
    // 联系人信息
    private SimpleAdapter simple = null ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        this.callList = 
             (ListView) super.findViewById(R.id.callList) ;
        // 查询通话记录
        this.result = super.getContentResolver().query(
                CallLog.Calls.CONTENT_URI, null, null, null, null);
        System.out.println("************ " 
              + this.result.getCount()) ;
        // 交给程序
        this.startManagingCursor(this.result) ;    
        this.allCalls = new ArrayList<Map<String,Object>>() ;
        // 取出数据
        for (this.result.moveToFirst(); !result.isAfterLast(); 
                result.moveToNext()) {    
            Map<String,Object> contact = 
                     new HashMap<String,Object>() ;
            // 设置一行的_id内容
            contact.put("_id",            result.getInt(result.getColumnIndex(CallLog.Calls._ID)));
            // 取出相匹配的联系人姓名
            String nameTemp = this.result.getString(
            this.result.getColumnIndex(CallLog.Calls.CACHED_NAME)) ;
            if (nameTemp == null || "".equals(nameTemp)) {
                nameTemp = "未知" ;
            }
            contact.put("name",nameTemp);
            contact.put("number",
                   result.getString(result.getColumnIndex(
                   CallLog.Calls.NUMBER)));
            this.allCalls.add(contact) ;
        }
        this.simple = new SimpleAdapter(this, this.allCalls, 
                R.layout.calls,
                new String[] { "_id", "name", "number" }, 
                new int[] { R.id._id,R.id.name, R.id.number });
        this.callList.setAdapter(this.simple) ;
    }
}
View Code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ContentProvider)