反应本地Firebase和Picker。我们如何将数据从Firebase加载到选择器

时间:2019-02-25 10:28:29

标签: android firebase react-native picker

Data From Firebase

我正在开发React本机应用程序。我想在选择器中显示来自Firebase的数据。 我该怎么证明?上面是数据库的图片。该类别是我要加载到选择器中的类别。我不知道该怎么做。我尝试了几种方法,但都失败了。在我的代码下方。谁能详细解释

import React from 'react';
import { StyleSheet, Text, View, TextInput, Picker } from 'react-native';
import DateTimePicker from 'react-native-modal-datetime-picker';
import moment from 'moment';
import * as firebase from 'firebase';
import {firebaseConfig} from './ApiKeys';
if (!firebase.apps.length) { firebase.initializeApp(firebaseConfig); }
console.ignoredYellowBox = ['Setting a timer',];
var serviceItems
export default class App extends React.Component {
  constructor(props){
    super(props)

    this.state=({ 

      isDateTimePickerVisible: false,
      selecteddate:'Date',
      category:[],
    })
  }

  componentDidMount() {
    this._loadInitialState();

  }
  _loadInitialState = () => {

  //====================================
  firebase.database().ref('category/').once('value').then((snapshot)=> {
    this.setState({category:snapshot})



  })
  //====================================
    };
    loadUserTypes() {
      return this.state.category.map(user => (
        <Picker.Item label={user.key} value={user.val()} />
      ))
    }


  _showDateTimePicker = () => this.setState({ isDateTimePickerVisible: true });

  _hideDateTimePicker = () => this.setState({ isDateTimePickerVisible: false });

  _handleDatePicked = (pickeddate) => {
    day   = pickeddate.getDate();
    month = pickeddate.getMonth();
    year  = pickeddate.getFullYear();
    console.log('A date has been picked: ' + day + '-' + month + '-' + year);
    exdate= moment(pickeddate).format('HH:mm')
    this.setState({selecteddate : exdate})
    this._hideDateTimePicker();
    console.log("cat "+ this.state.category.val())
  };

  onFocus = () => {
    this._handleDatePicked();
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text 

        onPress={ () => this._showDateTimePicker() }
        value={this.state.selecteddate}
        //editable={false} 
        //selectTextOnFocus={false}
        >{this.state.selecteddate}</Text>
        {/* //--------------------------------------DateTimePicker */}
        <DateTimePicker
                      isVisible={this.state.isDateTimePickerVisible}
                      onConfirm={this._handleDatePicked}
                      onCancel={this._hideDateTimePicker}
                      mode={'time'}
                      datePickerModeAndroid={'spinner'}


                    />
            {/* //-------------------------------------- */}


            {/* --------------Picker--------------------- */}
            <Picker
              selectedValue={this.state.language}
              style={{height: 50, width: 100}}
              onValueChange={(itemValue, itemIndex) =>
                this.setState({language: itemValue})
              }>
              {/* <Picker.Item label="Java" value="java" />
              <Picker.Item label="JavaScript" value="js" /> */}
              {/* -------------------Dynamic Picker Data------------------ */}
              {this.loadUserTypes()}
              {/* -------------------------------------------------------- */}
            </Picker>

          {/* --------------------------------------------- */}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

0 个答案:

没有答案