Content Provider 内容提供器

一、Content Provider 增删查改

    Cursor cursor=getContentResolver().query(
                uri, 
                projection,
                selection,
                selectionArgs, 
                sortOrder);

        //查询
        if(cursor!=null){
            while(cursor.moveToNext()){
                String column1=cursor.getString(cursor.getColumnIndex("column1"));
                int coloumn2=cursor.getInt(cursor.getColumnIndex("column2"));
            }
            cursor.close();
        }

        //插入
        ContentValues values=new ContentValues();
        values.put("column1", "");
        values.put("column2", 2);

        //更新
        ContentValues values=new ContentValues();
        values.put("column1", "");
        getContentResolver().update(uri, values, "column1=? and column2=?", new String[]{"text","2"});

        //删除
        getContentResolver().delete(url, "column2=?",
                new String[]{"1"});

二、获取联系人应用内容(实例)

    private void readContacts() {
        Cursor cursor=null;
        try {
            cursor=getContentResolver().query       (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, null, null, null);  //获取联系人应用中的信息
            while(cursor.moveToNext()){
                String name=cursor.getString(cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number=cursor.getString(cursor.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.NUMBER));

                contactLists.add(name+"\n"+number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(cursor!=null){
                cursor.close();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值