Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
790396054 opened this issue Aug 11, 2018 · 4 comments
Open

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

790396054 opened this issue Aug 11, 2018 · 4 comments

Comments

@790396054
Copy link

// 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
Copy link

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

@leobert-lan
Copy link
Contributor

leobert-lan commented Aug 14, 2018

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
Copy link

// 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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants