传递自定义对象的arraylist以从android响应本机端

传递自定义对象的arraylist以从android响应本机端,android,react-native,react-native-bridge,Android,React Native,React Native Bridge,我有一个自定义对象列表(列表)。我需要将此数据发送到React Native端,以显示在平面列表中。我如何做到这一点?此列表显示在 类NativeToReact(reactContext:ReactApplicationContext,userManager:IUserManager):ReactContextBaseJavaModule(reactContext)` public class NativeToReactModule extends ReactContextBaseJavaMod

我有一个自定义对象列表(列表)。我需要将此数据发送到React Native端,以显示在平面列表中。我如何做到这一点?此列表显示在


类NativeToReact(reactContext:ReactApplicationContext,userManager:IUserManager):ReactContextBaseJavaModule(reactContext)`

public class NativeToReactModule extends ReactContextBaseJavaModule {

  public NativeToReactModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "NativeToReactModule";
  }

  private static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
    WritableMap map = new WritableNativeMap();

    Iterator<String> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
      String key = iterator.next();
      Object value = jsonObject.get(key);
      if (value instanceof JSONObject) {
        map.putMap(key, convertJsonToMap((JSONObject) value));
      } else if (value instanceof Boolean) {
        map.putBoolean(key, (Boolean) value);
      } else if (value instanceof Integer) {
        map.putInt(key, (Integer) value);
      } else if (value instanceof Double) {
        map.putDouble(key, (Double) value);
      } else if (value instanceof String) {
        map.putString(key, (String) value);
      } else {
        map.putString(key, value.toString());
      }
    }
    return map;
  }

  @ReactMethod
  public void returnArrayOfObjects(Callback successCallback) throws JSONException {
    List<CustomObject> aList = new ArrayList<CustomObject>();
    Gson g = new Gson();

    aList.add(new CustomObject("Nameone", 1));
    aList.add(new CustomObject("nametwo", 132));

    WritableArray array = new WritableNativeArray();
    for (CustomObject co : aList) {
      JSONObject jo = new JSONObject(g.toJson(co));
      WritableMap wm = convertJsonToMap(jo);
      array.pushMap(wm);
    }

    successCallback.invoke(array);
  }
}
然后可以访问从本机端发送的
ArrayList

import NativeToReact from "./NativeToReactModule";

...
...
...

NativeToReact.returnArrayOfObjects(array => {
  console.log(array, "The array you sent from the native side");
});
注意:答案没有显示使用本机模块的完整设置。

从的答案展开(或减少?),您应该/可以使用FB的帮助器类来转换值

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.myapp.SomeClass;

public class NativeToReactModule extends ReactContextBaseJavaModule {

  public NativeToReactModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "NativeToReactModule";
  }

  @ReactMethod
  public void getArray(Callback successCallback) {
    WritableArray array = Arguments.fromList(SomeClass.SomeStaticListValue);
    successCallback.invoke(array);
  }
}
然后在JS端,只需调用:

import { NativeModules } from "react-native"

NativeModules.NativeToReactModule.getArray(array => console.log(array, "The array you sent from the native side"))
import { NativeModules } from "react-native"

NativeModules.NativeToReactModule.getArray(array => console.log(array, "The array you sent from the native side"))