ContentValue类用来保存一组值,方便ContentResolver处理。一般在数据库插入和更新数据的时候使用。
public long createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean updateNote(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
ContentValue类其实就是封装了HashMap的方法。ContentValue提供了各种数据类型的put和get方法,不需要我们自己进行类型转换了,HashMap还需要注意同步问题,使用多线程的时候注意。