Here am trying to get value in Picker , I am getting value in JSON format and trying to show that value in Picker like DropDown .
Please help. Below code am trying like hit and trial but not working .
Below is my code and JSON value also .
Do I need to install and dependencies ?
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View, Platform, Picker, ActivityIndicator, Button, Alert} from 'react-native';
export default class AddInventory extends Component {
componentDidMount() {
return fetch('http://1/Dsenze/userapi/inventory/viewinventorytype', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"username" :"admin",
"password" :"admin"
})
}).then((response) => response.json())
.then((responseJson) => {
var count = Object.keys(responseJson.message.Obj).length;
let PickerValueHolder = [];
for(var i=0;i<count;i++){
console.log(responseJson.message.Obj[i].name) // I need to add
PickerValueHolder.push({ value: responseJson.message.Obj[i].name }); // Create your array of data
}
this.setState({ PickerValueHolder }); // Set the new state
})
.catch((error) => {
console.error(error);
});
}
GetPickerSelectedItemValue=()=>{
Alert.alert(this.state.PickerValueHolder);
}
render() {
return (
<View style={styles.MainContainer}>
<Picker
selectedValue={this.state.PickerValueHolder}
onValueChange={(itemValue, itemIndex) => this.setState({PickerValueHolder: itemValue})} >
{ this.state.dataSource.map((item, key)=>(
<Picker.Item label={item.name} value={item.name} key={key} />)
)}
</Picker>
<Button title="Click Here To Get Picker Selected Item Value" onPress={ this.GetPickerSelectedItemValue } />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: 'center',
flex:1,
margin: 10
}
});
// below is JSON response
{
"inventoryTypeData": [{
"id": 1,
"name": "scanning equipment"
}, {
"id": 2,
"name": "ecg machine"
}, {
"id": 3,
"name": "ct-scan machine"
}, {
"id": 7,
"name": "x-ray machine"
}],
"success": "true"
}
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…