zoukankan      html  css  js  c++  java
  • 【数据存储】SQLite数据库存储(10) 操作通讯记录的ContentProvider

    <?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
  • 相关阅读:
    SharePoint 2010 不用工作流模拟工作流表单审批的解决方案
    SharePoint 2010 传入电子邮件和传出电子邮件做成收件箱和发件箱(理解可行性)
    SharePoint 2010 准备虚拟机开发环境出现的问题和解决方式
    使用Shell脚本对Linux系统和进程资源进行监控(转)
    你需要知道的16个Linux服务器监控命令(转)
    数字音频采样率与码率(转)
    ffmpeg libx264 编码 "use an encoding preset (e.g. vpre medium)" 错误解决
    一致性哈希算法consistent hashing
    TIME_WAIT 数量增加解决办法
    linux bc命令使用(转)
  • 原文地址:https://www.cnblogs.com/androidsj/p/3129610.html
Copyright © 2011-2022 走看看