ANDROID content provide 使用实例
Content Provider提供了一种多应用间数据共享的方式,比如:联系人信息可以被多个应用程序访问。Content Provider是个实现了一组用于提供其他应用程序存取数据的标准方法的类。
下面来看一个简单的实例: MainActivity.java
package com.app.contentprovidetest; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; public class MainActivity extends Activity {
//声明ContentResolver对象
private ContentResolver mContentResolver= null;
Cursor cursor ;
//声明联系人 Uri位置
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得ContentResolver对象
mContentResolver=getContentResolver();
uri =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//cursor取数据
//(uri地址,显示字段,查询条件,条件参数,排序)
cursor = mContentResolver.query(uri, null, null, null, null);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
if(cursor.getCount()>0){
while(cursor.moveToNext()){//游标下标移动
String name =cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
String phone =cursor.getString(cursor.getColumnIndex(Phone.DATA));
Log.i("xxxxxx", "name:"+name+","+"phone"+phone);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("phone", phone);
list.add(map);
}
ListView listView1 =(ListView) findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.listview_item,
new String[]{"name","phone"}, new int[]{R.id.textView1,R.id.textView2});
listView1.setAdapter(adapter);
}
insert();//添加系统ContentProvider 内容
}
private void insert() {
//获得ContentResolver对象实例
ContentResolver cr=getContentResolver();
//将添加的信息封装到ContentValues对象
ContentValues values = new ContentValues();
//定义Uri
Uri uri = Contacts.People.CONTENT_URI;
//添加姓名
values.put(People.NAME, "Android");
//添加电话号
values.put(People.NUMBER, "12345");
//执行添加操作
cr.insert(uri, values);
Toast.makeText(getApplicationContext(), "添加成功" , Toast.LENGTH_SHORT).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
R.Layout.XML文件 listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout> </LinearLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</ListView> </RelativeLayout>
ANDROID content provide 使用实例的更多相关文章
- Android应用程序组件Content Provider应用实例
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...
- Android Content Provider Guides
Android Content Provider Guides Content Providers管理对结构化数据集的访问.它们包装数据,并且提供一种定义数据安全的机制. Content provid ...
- Android Content Provider基础
Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...
- (转载)Android content provider基础与使用
android有一个独特之处就是,数据库只能被它的创建者所使用,其他的应用是不能访问到的,所以如果你想实现不同应用之间的数据共享,就不得不用content provider了.在Android中,co ...
- android.content.Context 含义及使用
Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Con ...
- Android传感器编程带实例
看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推荐大家也看看.话不多说,还是言归正传吧. 一.前言 我很喜欢电脑,可是笔记本还是太大,笔记本电脑再小还是要 ...
- Android传感器编程带实例(转)
源:http://www.cnblogs.com/xiaochao1234/p/3894751.html 看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推 ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- 6、Android Content Provider测试
如果你的应用中使用了Content Provider来与其他应用进行数据交互,你需要对Content Provider进行测试来确保正常工作. 创建Content Provider整合测试 在Andr ...
随机推荐
- Linux下安装jdk+maven +git
Linux系统下的操作,一直不是很熟悉.作为一名java开发工程师,感到很惭愧.因此把自己的阿里云服务器安装环境相关的东西给记录下来,方便后续查阅. 本文所采用的Lin ...
- Luogu P1966 火柴排队
这还是一道比较简单的题目,稍微想一下就可以解决.终于有NOIP难度的题目了 首先我们看那个∑(ai-bi)^2的式子,发现这个的最小值就是排序不等式 所以我们只需要改变第一组火柴的顺序,使它和第二组火 ...
- 北航学堂Android客户端Beta阶段测试报告
我们已经知道的bug如下: 1.在没有网络的情况下,我们的程序会直接崩溃,没有弹出提醒网络异常的错误,这是个比较严重的bug,我们在6号7号 考试结束之后会进行修改: 有待进行的优化: 1.UI界面的 ...
- Java计算器(结对)
一:题目简介 我们要做的是一个多功能计算器,Java程序编辑器是:图形界面.线程.流与文件等技术的综合应用. 图形界面的实现:考虑到简单.实用.高效等特点,就选择了Swing来完成实现,在选择组件上, ...
- pandas修改全列的时间格式 无需使用apply
df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式
- DockerHub使用简介
常用的Docker镜像文件都有,就不用自己费劲的一点点配置了,这才是Docker的真正目的.就像Ghost里边含office,直接还原,不用一台台机器安装呢,省时省力,与高效工作的理念相契合. 至于, ...
- dip vs di vs ioc
https://stackoverflow.com/questions/6766056/dip-vs-di-vs-ioc https://docs.microsoft.com/en-us/aspnet ...
- eclipse集成tomcat日志文件输出配置
eclipse集成tomcat日志文件输入配置 2015-07-21 00:13 1072人阅读 评论(0) 收藏 举报 分类: tomcat(1) eclipse Where can I vie ...
- Typecho博客迁移
在新的机器上先搭建好一个新的Typecho博客,数据库名称和原博客相同(可以省不少事). 备份原来博客的usr目录. 备份mysql数据库,命令: mysqldump -uroot -p --all- ...
- [转帖] 从零开始编写自己的C#框架(27)——什么是开发框架
从零开始编写自己的C#框架(27)——什么是开发框架 http://www.cnblogs.com/EmptyFS/p/4105713.html 没写过代码 一直不清楚 框架的含义 不过看了一遍 也没 ...