如何使用api响应在本机中填充listview的数据源?

时间:2017-10-20 14:49:29

标签: listview react-native

My Api给我的回复与下面定义的const“devices”完全相同。如果我不调用api,我正在使用单行数据正确填充listview,但如果我使用Api响应填充数据源,我将获得空行160行。

我的setState出了什么问题?

    const devices = [
        {
            "DeviceID": 4,
            "DeviceCode": "ODPRC-HB-4",
            "ContactName": "Sudhir",
            "GPS_Lat_Long": "15.396863, 74.012382",
            "DeviceName": "Sudhirs Device"
        }
    ];

    class Devices extends Component {

    constructor(props) {
        super(props);
        const ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 !== r2,
        });
        this.state = {
            dataSource: ds.cloneWithRows(devices)
        }
    }
    componentDidMount() {
        this.getDeviceList();
    }

    getDeviceList() {
        let request = new Request('https://myapiWithQueryString.com');
        request.addHeaders({ 'Content-Type': 'application/json'});  
        RNETWORK.get(request, () => {
        }, (response) => {
            if (response.getError()) {
                alert(response.getError().message);
            } else {
                this.setState({
                    dataSource: this.state.dataSource.cloneWithRows(response.getBodyString())

                });
                alert(response.getBodyString());
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

试试这个:

.NSMetadataQueryDidFinishGathering

注意:我假设getBodyString实际上返回了一些东西,因为我不记得响应接口中存在的那个方法。