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
  • 相关阅读:
    Ceres求解直接法BA实现自动求导
    栈溢出漏洞原理详解与利用
    【机器学习】使用CNN神经网络实现对图片分类识别及模型转换
    【Android】java中调用JS的方法
    5 项大奖,70 项满分!阿里云全方位引领云原生技术升级
    分布式系统架构与云原生—阿里云《云原生架构白皮书》导读
    Dubbo-go 发布 1.5 版,朝云原生迈出关键一步
    阿里产品专家:高情商的技术人,如何做沟通?
    CNCF 新增两个孵化项目 | 云原生生态周报 Vol. 58
    掌门教育微服务体系 Solar | 阿里巴巴 Nacos 企业级落地上篇
  • 原文地址:https://www.cnblogs.com/androidsj/p/3129610.html
Copyright © 2011-2022 走看看