如何将ReactMethod(Android Native)转换为模型类(User.class)

时间:2019-05-24 08:15:05

标签: android react-native react-native-android

我正在构建一个自然响应程序,该程序在原始Android中具有小的模块。我将一个对象传递给了原生android  我想将ReadableMap (@ReactMethod)转换为model.java (User.java)

我正在使用的本机版本是:

"react-native": "^ 0.55.4"

这是传递对象时的代码:

this.filter = {
                name: 'Xuan Du',
                age: 21,
                address: 'Ho Chi Minh'
               }
let activityStarter = NativeModules.ActivityReceiverParamsStarter;
activityStarter.navigateToReceiverParams(this.filter)

这是我的用户模型:

public class User {

    private String name, address;
    private Integer age;

    public User(ReadableMap user) {
        this.name = user.getString("name");
        this.address = user.getString("address");
        this.age = user.getInt("age");
    }
    ...
    getter setter
}

这是代码android native:

@ReactMethod
    void navigateToReceiverParams(ReadableMap map) {
        ReactApplicationContext context = getReactApplicationContext();
        User user = new User(map);
    }

我当前的代码已使user.class拥有数据,但必须通过构造函数。

我想要这样的东西而无需在构造函数中进行处理:

User user = (User) map;

0 个答案:

没有答案