如何获取Firebase数据,将其存储到数组中,然后使用平面列表显示它?

时间:2019-09-23 14:36:45

标签: firebase react-native react-native-flatlist

我正在从Firebase提取数据并将其存储到数组中。现在我想使用平面列表显示它们,但我不知道如何?

class HomeScreen extends Component{

  constructor(props){
    super(props);
    this.state={
      menu:[]
    }
  }

  // componentDidMount() {
  //   firebase.auth().onAuthStateChanged(authenticate => {
  //     if (authenticate) {
  //       this.props.navigation.replace("Home");
  //     } else {
  //       this.props.navigation.replace("login");
  //     }
  //   });
  // }

   componentDidMount() {
    firebase.database().ref('menu/starter/').once('value').then(snapshot => {
       var items = [];
       snapshot.forEach((child) => {
         items.push({
            name: child.val().name,
            image: child.val().image,
            price: child.val().price,
         });
      });
      this.setState({ menu: items});
      console.log(this.state.menu)
  });
  }

  render(){
    return(
     <View style={styles.container}>
     <Text></Text>
     </View>
    )
  }
}

1 个答案:

答案 0 :(得分:0)

import openpyxl
import pprint

data = {}
files = {'My_main_file':'my_file.xlsx',
         'File_WholesalerA':'FileA.xlsx',
         'File_WholesalerB':'FileB.xlsx'
         }

wb1 = openpyxl.load_workbook(files['My_main_file'])                   
wb2 = openpyxl.load_workbook(files['File_WholesalerA'])                
wb3 = openpyxl.load_workbook(files['File_WholesalerB'])           
sheet1 = wb1.get_sheet_by_name('Master Database')
sheet2 = wb2.get_sheet_by_name('sheetA')
sheet3 = wb3.get_sheet_by_name('sheetB')

# Collect all product codes in my database spreadsheet and add them as keys to the empty dictionary
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    data[code] = code

# Get Wholesaler A prices and add them to data dictionary
for row in range(2, sheet2.max_row + 1):
    code = sheet2['A' + str(row)].value
    if code in data:
        data[code]['price'] = sheet2['J' + str(row)].value
        data[code]['seller'] = 'Wholesaler A'

# Get Wholesaler B prices and add them to prices dictionary
for row in range(2, sheet3.max_row + 1):
    code = sheet3['A' + str(row)].value
    if code in data:
        data[code]['price'] = sheet3['K' + str(row)].value
        data[code]['seller'] = 'Wholesaler B'

# Paste the prices collected into the dictionary into my excel sheet for each #corresponding product code
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    if code in data:
        # Here I try to ensure that the code only updates the prices for the 
        # corresponding sellers and doesn't overwrite the prices for 
        # manufacturers.
        if sheet1['C' + str(row)].value == data[code]['seller']:
            sheet1['K' + str(row)].value = data[code]['price']

# Save another version of the spreadsheet with the data
wb1.save('My_main_file v2.xlsx')

pprint.pprint(data)

renderItem是呈现每个数组项的回调