当前位置: 首页 > news >正文

泉州网站建设电话ps素材库

泉州网站建设电话,ps素材库,wordpress get_tax_meta,建设部网站公告注册成功Cursor这个类是Android开发者难以避免的#xff0c;比如数据库、ContentResolver内容的读取#xff0c;但通过这个类读取内容非常的繁琐#xff0c;针对要读取的每一个字段都会有这样一段代码#xff1a; int idIndex cursor.getColumnIndex(id); //获取字段…Cursor这个类是Android开发者难以避免的比如数据库、ContentResolver内容的读取但通过这个类读取内容非常的繁琐针对要读取的每一个字段都会有这样一段代码 int idIndex cursor.getColumnIndex(id); //获取字段对应的列index列index通常并不需要每次都获取 if(idIndex 0){ //判断列index的合法性String id cursor.getString(idIndex); //获取对应列的内容 }这种代码基本没法复用而且还都是纯手工代码自动生成比较麻烦我希望可以像用json映射那样每个字段/列一行代码就完成这个任务所以本文就仿照以前解构Bundle一样来解构Cursor完整实现差不多100行。 实现效果 以MediaStore读取照片为例先编写内容要映射到的Java数据类重点在于其中的CursorContract public class SystemMedia implements Serializable {private long id;private String data;private long size;private String displayName;private String mimeType;private long dateAdded;private long dateModified;private long bucketId;private String bucketDisplayName;private String album;private int height;private int width;private int orientation;public interface CursorContract { //重点这个类声明映射的合约需要提供一个同样参数的构造方法以方便使用SystemMedia consume(Key(MediaStore.MediaColumns._ID) long id,Key(MediaStore.MediaColumns.DATA) String data,Key(MediaStore.MediaColumns.SIZE) long size,Key(MediaStore.MediaColumns.DISPLAY_NAME) String displayName,Key(MediaStore.MediaColumns.MIME_TYPE) String mimeType,Key(MediaStore.MediaColumns.DATE_ADDED) long dateAdded,Key(MediaStore.MediaColumns.DATE_MODIFIED) long dateModified,Key(MediaStore.MediaColumns.BUCKET_ID) long bucketId,Key(MediaStore.MediaColumns.BUCKET_DISPLAY_NAME) String bucketDisplayName,Key(MediaStore.MediaColumns.HEIGHT) int height,Key(MediaStore.MediaColumns.WIDTH) int width,Key(MediaStore.MediaColumns.ALBUM) String album,Key(MediaStore.MediaColumns.ORIENTATION) int orientation);}public SystemMedia(long id, String data, long size, String displayName, String mimeType, long dateAdded, long dateModified, long bucketId, String bucketDisplayName, int height, int width, String album, int orientation) {this.id id;this.data data;this.size size;this.displayName displayName;this.mimeType mimeType;this.dateAdded dateAdded;this.dateModified dateModified;this.bucketId bucketId;this.bucketDisplayName bucketDisplayName;this.height height;this.width width;this.album album;this.orientation orientation;}public SystemMedia() {}//省略 getter 和 setter }然后我们的查询代码就变成了 public void query(Context context) {try (Cursor cursor context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,null, null, null)) {if (cursor ! null) {ListSystemMedia result new ArrayList();while (cursor.moveToNext()) {SystemMedia media (SystemMedia) CursorAdapter.withCursor(cursor, SystemMedia.CursorContract.class, SystemMedia::new);result.add(media);}}} }这样就结束了。 API说明 CursorAdapter.withCursor 方法 第一个Cursor参数目标Cursor对象第二个Class参数解构的合约接口需要是单方法的函数式接口类名和方法名随意该方法的参数需要使用Key注解第三个T参数合约接口的实现类完成由所有字段到对象的转换通常为全属性的构造方法的方法引用比如SystemMedia::new Key 注解 用于标注参数对应的字段名Cursor列名 完整实现差不多100行 其中用到的Memorizer工具见Java小技巧创建带缓存的过程 public abstract class CursorAdapterT {abstract T read(Cursor cursor, int index);public static T Object withCursor(Cursor cursor, ClassT clazz, T t) {ListPairInteger, CursorAdapter? list CursorAdapter.adapterExtractor.apply(cursor).apply(clazz);Method[] methods clazz.getMethods();if (methods.length 1) {Object[] args list.stream().map(pair - pair.first 0 ? pair.second.read(cursor, pair.first) : null).toArray();try {return methods[0].invoke(t, args);} catch (IllegalAccessException | InvocationTargetException e) {throw new RuntimeException(e);}} else {throw new IllegalStateException(methods length is not 1, current is methods.length);}}static final FunctionCursor, FunctionClass?, ListPairInteger, CursorAdapter? adapterExtractor Memorizer.weakMemorize(cursor - Memorizer.memorize(clazz - {Method[] methods clazz.getMethods();if (methods.length 1) {Method desMethod methods[0];Annotation[][] parameterAnnotations desMethod.getParameterAnnotations();Type[] parameterTypes desMethod.getGenericParameterTypes();if (parameterTypes.length parameterAnnotations.length) {ListPairInteger, CursorAdapter? adapterList new LinkedList();for (int i 0; i parameterTypes.length; i) {Type parameterType parameterTypes[i];OptionalPairInteger, CursorAdapter? pairOptional Arrays.stream(parameterAnnotations[i]).filter(annotation - annotation instanceof Key).map(annotation - (Key) annotation).findFirst().map(key - new Pair(cursor.getColumnIndex(key.value()), CursorAdapter.adapterBuilder.apply(parameterType)));if (pairOptional.isPresent()) {adapterList.add(pairOptional.get());} else {throw new IllegalStateException(every parameter must contains a Key annotation);}}return adapterList;} else {throw new IllegalStateException(parameters length is not equal to annotations length);}} else {throw new IllegalArgumentException(methods size must be 1, current is methods.length);}}));private static final FunctionType, CursorAdapter? adapterBuilder Memorizer.memorize(type - {if (int.class.equals(type) || Integer.class.equals(type)) {return create(Cursor::getInt);} else if (float.class.equals(type) || Float.class.equals(type)) {return create(Cursor::getFloat);} else if (long.class.equals(type) || Long.class.equals(type)) {return create(Cursor::getLong);} else if (short.class.equals(type) || Short.class.equals(type)) {return create(Cursor::getShort);} else if (String.class.equals(type)) {return create(Cursor::getString);} else if (double.class.equals(type) || Double.class.equals(type)) {return create(Cursor::getDouble);} else if (type instanceof GenericArrayType) {Type componentType ((GenericArrayType) type).getGenericComponentType();if (byte.class.equals(componentType)) {return create(Cursor::getBlob);} else {throw new IllegalStateException(unsupported componentType: componentType);}} else {throw new IllegalArgumentException(unsupported type : type);}});private static T CursorAdapterT create(BiFunctionCursor, Integer, T reader) {return new CursorAdapterT() {OverrideT read(Cursor cursor, int index) {return reader.apply(cursor, index);}};} }
http://www.zqtcl.cn/news/19985/

相关文章:

  • 提供网站建设设计外包php自己做网站吗
  • 网站网站平台建设方案影视投资销售怎么找客户
  • 通过备案号查网站河南网络建站
  • 凯里网站设计公司乐陵森林面积
  • 网站推广 网站阿里巴巴有几个网站是做外贸的
  • 门户网站定制做网站优化的
  • 把网站制作成app免费网站建设网站开发公司
  • 小程序建站公司手机网站建设模板下载
  • 网站建设维护单位襄阳做网站排行榜
  • 江西网站开发科技公司网页设计及网站建设的相关概念
  • 广西网站怎么制作世界建筑设计公司排名
  • 在线教育网站建设wordpress新增php页面
  • chinaz站长素材网站开发整体流程
  • 泰宁县建设局网站电子商务网站建设移动电商开发
  • 简易的网站建设管理者应具备的能力
  • 网站开发生命周期模型泰安网络推广诚信臻动传媒
  • 网站开发项目报告网站制作公司哪家价钱合理
  • 网站建设合同怎么交印花税佛山网站建设外包
  • 公司网站怎么建设ui设计工作流程
  • 网站登录窗口怎么做wordpress 编辑器按钮
  • 怎么建设个人网站 新手学做网站微信服务平台开发
  • 苏州手机网站建设百度seo是什么意思
  • 网站开发的基本流程华为开发平台
  • 做网站跟app网站开发服务合同
  • 蛋糕网站设计建正建设官方网站
  • 杭州网站设计的公司单页面网站模板怎么做
  • 网站的外链是什么上杭县城乡规划建设局网站
  • 南昌网站小程序开发中国建设银行网站首页旧版
  • lol网站怎么做外贸手工做兼职的网站
  • 网站建设综合案例相亲网站怎么建设