您的位置:首页 > 产品设计 > UI/UE

ContentValues(Java)

2015-12-13 19:32 337 查看
在SDK中,ContentValues的介绍为:

This class is used to store a set of values that the ContentResolver can process.

就是用于保存一些数据(string/boolean/byte/double/float/int/long/short …)信息,这些信息可以被数据库操作时方便地使用。

ContentValues 和 HashTable 类似,都是一种存储的机制,但是两者最大的区别就在于:ContentValues 只能存储基本类型的数据,像string、int之类的,不能存储对象这种东西,而HashTable却可以存储对象。

一些常用的方法如下:

ContentValues() Creates an empty set of values using the default initial size


ContentValues(int size) Creates an empty set of values using the given initial size


ContentValues(ContentValues from) Creates a set of values copied from the given set


比如向 SQLite 数据库中插入数据的时候,首先应该有一个ContentValues的对象:

ContentValues cv = new ContentValues(); //创建对象
cv.put(key,values);  // 赋值
SQLiteDataBase sdb ; // 创建数据库
sdb.insert(database_name,null,initialValues); // 将数据插入数据库


插入成功就返回记录的id,否则返回-1.

说明:ContentValues是基于HashMap的,这与HashTable是不同的。

下面简要说一下HashMap与HashTable的区别:

Hashtable 继承自 Dictiionary 而 HashMap继承自AbstractMap。

Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。

Hashtable中,key和value都不允许出现null值。在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: