如何从React Native Component中的Java模块返回数组?

时间:2017-09-25 21:32:58

标签: java android-studio react-native native-module

我写这篇文章是为了回答我自己的问题,因为文档没有明确说明这一点,我无法在堆栈溢出或其他任何地方找到它。以下是如何将一个String数组从Java模块返回到React Native Component(从我个人项目中的代码中简化)。

CPPConnection.java文件

public class CPPConnection extends ReactContextBaseJavaModule {
@ReactMethod
    public void GetDerivedUnits(String scheme,final Promise promise){
        try {

            List<String> returnSet = new ArrayList<String>();
            //Set first value so that there is a lead value
            returnSet.add("");
            returnSet.add(scheme);
            returnSet.add("lb");
            returnSet.add("kg");
            String[] returnArray = new String[returnSet.size()];
            returnArray = returnSet.toArray(returnArray);

            WritableArray promiseArray=Arguments.createArray();
            for(int i=0;i<returnArray.length;i++){
                promiseArray.pushString(returnArray[i]);
            }

            promise.resolve(promiseArray);
        }
        catch(Exception e){
            promise.reject(e);
        }
    }
}

Density.js文件

export default class Density extends Component{
...
constructor(props) {
    super(props);
    this.state = {
      mass: '',
      massUnits:[],
      volume: '',
      volumeUnits:[],
      density: 'N/A',
    };
    this.GetDerivedUnits("M",this.state.massUnits);
    this.GetDerivedUnits("L3",this.state.volumeUnits);
}

GetDerivedUnits= async (scheme,unitArray)=>{
    try{

        let asyncUnitSet = await CPPConnection.GetDerivedUnits(scheme);


        for (let i=0;i<asyncUnitSet.length;i++){
            unitArray.push(asyncUnitSet[i])
            this.setState({
                        unitArray: unitArray
                    })

        }

        console.log(asyncUnitSet);
    }catch(e){
        console.error(e);
    }
}
...
}

2 个答案:

答案 0 :(得分:6)

我写这篇文章是为了回答我自己的问题,因为文档没有明确说明这一点,我无法在堆栈溢出或其他任何地方找到它。以下是如何将一个String数组从Java模块返回到React Native Component(从我个人项目中的代码中简化)。

CPPConnection.java文件

public class CPPConnection extends ReactContextBaseJavaModule {
@ReactMethod
    public void GetDerivedUnits(String scheme,final Promise promise){
        try {

            List<String> returnSet = new ArrayList<String>();
            //Set first value so that there is a lead value
            returnSet.add("");
            returnSet.add(scheme);
            returnSet.add("lb");
            returnSet.add("kg");
            String[] returnArray = new String[returnSet.size()];
            returnArray = returnSet.toArray(returnArray);

            WritableArray promiseArray=Arguments.createArray();
            for(int i=0;i<returnArray.length;i++){
                promiseArray.pushString(returnArray[i]);
            }

            promise.resolve(promiseArray);
        }
        catch(Exception e){
            promise.reject(e);
        }
    }
}

Density.js文件

export default class Density extends Component{
...
constructor(props) {
    super(props);
    this.state = {
      mass: '',
      massUnits:[],
      volume: '',
      volumeUnits:[],
      density: 'N/A',
    };
    this.GetDerivedUnits("M",this.state.massUnits);
    this.GetDerivedUnits("L3",this.state.volumeUnits);
}

GetDerivedUnits= async (scheme,unitArray)=>{
    try{

        let asyncUnitSet = await CPPConnection.GetDerivedUnits(scheme);


        for (let i=0;i<asyncUnitSet.length;i++){
            unitArray.push(asyncUnitSet[i])
            this.setState({
                        unitArray: unitArray
                    })

        }

        console.log(asyncUnitSet);
    }catch(e){
        console.error(e);
    }
}
...
}

答案 1 :(得分:-1)

您可以按照ReadableArray所述使用here