如何解决在选择器中反应本机空值?

时间:2019-12-23 22:04:28

标签: android react-native view dropdown

我一直在研究Android平台上的react native应用程序。我实现了一个DropDown选择器,但它给出了一个错误。

给出错误的代码段:

<View style={styles.if_filterbox}>
    <View style={{ alignSelf: 'center', paddingTop: 5 }}>
        <Text style={styles.if_formLabel}>Adres Seçiniz</Text>
    </View>


    <View style={{ flex: 1, flexDirection: 'column' }}>
        <View style={{ flex: 0.5, flexDirection: 'row' }}>


        <View style={styles.if_regionselect}>
            <Picker
            selectedValue={this.state.cityID}
            style={styles.if_picker}
            mode="dropdown"
            onValueChange={(itemValue, itemIndex) => {
                if (itemValue !== 0) {
                this.setState({
                    city: this.state.cities[itemValue],
                })
                }
            }
            }>
            {this.checkIsCitySelected()}
            {Object.keys(this.state.cities).map(key => {
                return <Picker.Item label={this.state.cities[key].isim} value={key} />
            })}
            </Picker>
        </View>
        </View>
    </View>
</View>  

checkIsCitySelected = () => {
    if (this.state.cityID >= 0) {
      return (<Picker.Item label={this.state.city["isim"]} value={this.state.cityID} />)
    } else {
      return (<Picker.Item label='Şehir Seçiniz' value='-1' />)
    }
  }

我知道城市数组不为空。

错误: Android Dropdown Error

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。在选择器中,值是“键”。我将值更改为城市ID。在这种情况下,城市ID为:this.state.cities[key].sehir_no

代码块更改为

<View style={styles.if_regionselect}>
    <Picker
    selectedValue={this.state.cityID}
    style={styles.if_picker}
    mode="dropdown"
    onValueChange={(itemValue, itemIndex) => {
        if (itemValue !== 0) {
        this.setState({
            city: this.state.cities[itemValue],
            cityID: itemValue,
        })
        }
    }
    }>
    {Object.keys(this.state.cities).map(key => {
        return <Picker.Item label={this.state.cities[key].isim} value={this.state.cities[key].sehir_no} />
    })}
    </Picker>
</View>