luojilab / DDComponentForAndroid

一套完整有效的android组件化方案,支持组件的组件完全隔离、单独调试、集成调试、组件交互、UI跳转、动态加载卸载等功能
MIT License
3.34k stars 570 forks source link

UI 跳转 List 数据的传递怎么使用? #115

Open 790396054 opened 6 years ago

790396054 commented 6 years ago

// UI transfer with Bundle private void goToShareActivityWithBundle() { Author author = new Author(); author.setName("Margaret Mitchell"); author.setCounty("USA"); Bundle bundle = new Bundle(); bundle.putString("bookName", "Gone with the Wind"); bundle.putString("author", JsonService.Factory.getInstance().create().toJsonString(author)); UIRouter.getInstance().openUri(getActivity(), "DDComp://share/shareBook", bundle); }

这是 Demo 中的数据传递,如果传递 List 数据,怎么传递? @Autowired List author; 接收数据时,这么写,编译报错:错误: 需要<标识符>

@Override public void inject(Object target) { jsonService = JsonService.Factory.getInstance().create(); SecondActivity substitute = (SecondActivity)target; if (null != jsonService) { substitute.children = jsonService.parseObject(substitute.getIntent().getStringExtra("children"), List.class); } else { Log.e("AutowiredProcessor", "You want automatic inject the field 'children' in class 'SecondActivity' , but JsonService not found in Router"); } substitute.title = substitute.getIntent().getStringExtra("title"); }

这是编译生成的类,List.class 这个地方编译不通过,Cannot select from parameterized type

shiweibsw commented 6 years ago

List可以直接通过bundle传递,List 可以通过Parcelable

leobert-lan commented 6 years ago

DDComp这边代码很久没有维护过了,记忆中,在使用Autowired的时候,当时提供的功能并不是很多。 在JIMU那边最新的代码里,我也仅提供了这些支持:

// Primitive
        if (typeMirror.getKind().isPrimitive()) {
            return element.asType().getKind().ordinal();
        }

        switch (typeMirror.toString()) {
            case Constants.BYTE:
                return Type.BYTE.ordinal();
            case Constants.SHORT:
                return Type.SHORT.ordinal();
            case Constants.INTEGER:
                return Type.INT.ordinal();
            case Constants.LONG:
                return Type.LONG.ordinal();
            case Constants.FLOAT:
                return Type.FLOAT.ordinal();
            case Constants.DOUBEL:
                return Type.DOUBLE.ordinal();
            case Constants.BOOLEAN:
                return Type.BOOLEAN.ordinal();
            case Constants.STRING:
                return Type.STRING.ordinal();
            default:    // Other side, maybe the PARCELABLE or OBJECT.
                if (types.isSubtype(typeMirror, parcelableType)) {  // PARCELABLE
                    return Type.PARCELABLE.ordinal();
                }
//                else if (types.isSubtype(typeMirror, serializableType)) {
//                    return Type.SERIALIZABLE.ordinal(); 暂不支持需要做转型
//                }
                else {    // For others
                    return Type.OBJECT.ordinal();
                }

显然,这边只会走到OBJECT的处理逻辑中,也就是利用json工具做序列化/反序列化.即使用Bundle#putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)的形式去传递,也无法用Autowired取出来并赋值。只能自己去Intent的Bundle中读取。或者用一个对象类去包裹一下这个List(注意需要去泛化)


PS:经过长时间的考虑,我越发觉得UI路由是个有点花里胡哨的功能。但是自动取值还是比较有意义的,在我另一个项目中,是尝试过对绝大多数Bundle中支持的数据做智能推断的。目前可以支持这么多(根据名字推测一下): image 有空的话我会考虑下扩展自动取值注入的功能,代码应该会在JIMU仓库提交。

xiaojinzi123 commented 5 years ago

// UI transfer with Bundle private void goToShareActivityWithBundle() { Author author = new Author(); author.setName("Margaret Mitchell"); author.setCounty("USA"); Bundle bundle = new Bundle(); bundle.putString("bookName", "Gone with the Wind"); bundle.putString("author", JsonService.Factory.getInstance().create().toJsonString(author)); UIRouter.getInstance().openUri(getActivity(), "DDComp://share/shareBook", bundle); }

这是 Demo 中的数据传递,如果传递 List 数据,怎么传递? @Autowired List author; 接收数据时,这么写,编译报错:错误: 需要<标识符>

@override public void inject(Object target) { jsonService = JsonService.Factory.getInstance().create(); SecondActivity substitute = (SecondActivity)target; if (null != jsonService) { substitute.children = jsonService.parseObject(substitute.getIntent().getStringExtra("children"), List.class); } else { Log.e("AutowiredProcessor", "You want automatic inject the field 'children' in class 'SecondActivity' , but JsonService not found in Router"); } substitute.title = substitute.getIntent().getStringExtra("title"); }

这是编译生成的类,List.class 这个地方编译不通过,Cannot select from parameterized type

DDComponent 很久没维护了,其实问题还是挺多的,建议使用下面的方案,持续更新,框架贴近系统,不使用晦涩难懂的配置,并且功能强大 Component

xiaojinzi123 commented 5 years ago

DDComp这边代码很久没有维护过了,记忆中,在使用Autowired的时候,当时提供的功能并不是很多。 在JIMU那边最新的代码里,我也仅提供了这些支持:

// Primitive
        if (typeMirror.getKind().isPrimitive()) {
            return element.asType().getKind().ordinal();
        }

        switch (typeMirror.toString()) {
            case Constants.BYTE:
                return Type.BYTE.ordinal();
            case Constants.SHORT:
                return Type.SHORT.ordinal();
            case Constants.INTEGER:
                return Type.INT.ordinal();
            case Constants.LONG:
                return Type.LONG.ordinal();
            case Constants.FLOAT:
                return Type.FLOAT.ordinal();
            case Constants.DOUBEL:
                return Type.DOUBLE.ordinal();
            case Constants.BOOLEAN:
                return Type.BOOLEAN.ordinal();
            case Constants.STRING:
                return Type.STRING.ordinal();
            default:    // Other side, maybe the PARCELABLE or OBJECT.
                if (types.isSubtype(typeMirror, parcelableType)) {  // PARCELABLE
                    return Type.PARCELABLE.ordinal();
                }
//                else if (types.isSubtype(typeMirror, serializableType)) {
//                    return Type.SERIALIZABLE.ordinal(); 暂不支持需要做转型
//                }
                else {    // For others
                    return Type.OBJECT.ordinal();
                }

显然,这边只会走到OBJECT的处理逻辑中,也就是利用json工具做序列化/反序列化.即使用Bundle#putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)的形式去传递,也无法用Autowired取出来并赋值。只能自己去Intent的Bundle中读取。或者用一个对象类去包裹一下这个List(注意需要去泛化)

PS:经过长时间的考虑,我越发觉得UI路由是个有点花里胡哨的功能。但是自动取值还是比较有意义的,在我另一个项目中,是尝试过对绝大多数Bundle中支持的数据做智能推断的。目前可以支持这么多(根据名字推测一下): image 有空的话我会考虑下扩展自动取值注入的功能,代码应该会在JIMU仓库提交。

使用这个吧,你不会失望的 Component