ContentProvider(内容提供者)简单运用

1、配置清单添加需要的权限

代码

 

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

 

========================

 

 

2、布局文件

 

activity_main.xml布局文件

 

代码

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView_main_calllist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <TextView
        android:id="@+id/text_main_empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#00f"
        android:textSize="24sp"
        android:gravity="center"
        android:text="暂无通话记录信息!" />

</LinearLayout>

 

-------------------------------------

 

 

item_listview.xml布局文件

 

代码

 

 

<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/text_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/text_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/text_item_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

 

 

========================================

 

 

3、MainActivity 类

 

代码

 

 

public class MainActivity extends Activity {

 private ListView listview;
 private SimpleAdapter adapter;
 private TextView text_empty;
 private String uri_call = "content://call_log/calls";//访问电话记录的路径

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  this.listview = (ListView) this
    .findViewById(R.id.listView_main_calllist);
  this.text_empty = (TextView) this.findViewById(R.id.text_main_empty);
  listview.setEmptyView(text_empty);


/*使用ContentResolver 管理联系人:
 *(一)、 使用ContentResolver 操作数据的步骤:
1、调用Context的getContentResolver()方法获得ContentResolver 对象
2、调用使用ContentResolver 的insert()、delete()、update()、query()方法操作数据。
Uri insert(Uri uri, ContentValues values)
int delete(Uri uri, String where, String[] whereArgs)
int update(Uri uri, ContentValues values, String where, String[] whereArgs)
Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, String sortOrder)
参数解释:
String where:表示带有占位符的where子句组成的字符串;
String[] whereArgs:表示替换where参数中占位符后的数据组成的字符串数组;
String sortOrder:表示select语句中的order by子句组成的字符串;
String[] projection:表示select语句中需要查询的所有的字段组成的字符串数组。
ContentValues values:是由数据库中表字段和往该字段中放置的数据所组成的键值对对象。
【备注:】以上四个方法的参数分别是2、3、4、5个。*/
  ContentResolver resolver = getContentResolver();
  Cursor cursor = resolver.query(Uri.parse(uri_call), new String[] {
    "_id", "number", "date", "type" }, null, null, null);
  
  List<Map<String, String>> list_calldata = select(cursor);
  adapter = new SimpleAdapter(this, list_calldata, R.layout.item_listview,
    new String[] {"number","date","type"}, new int[] {R.id.text_item_number,R.id.text_item_date,R.id.text_item_type});
  listview.setAdapter(adapter);
 }
 
 public List<Map<String, String>> select(Cursor cursor){
  List<Map<String, String>> list = new ArrayList<Map<String,String>>();
  while(cursor.moveToNext()){//数据库表的 行
   Log.i("data", "dsfasfa" );
   Map<String, String> map = new HashMap<String, String>();
   for(int i = 0;i<cursor.getColumnCount();i++){//数据库表的列
    map.put(cursor.getColumnName(i), cursor.getString(i));
   }
   list.add(map);
  }
  cursor.close();
  return list;
 }
}

转载于:https://my.oschina.net/u/2542711/blog/608659

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值