Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { StyleSheet, Text, View, Image, FlatList, Icon, Alert } from 'react-native';
  3. import { List, ListItem } from 'react-native-elements';
  4. import * as Constants from '../Constants';
  5.  
  6. // genero la conexion
  7. var SQLite = require('react-native-sqlite-storage')
  8. var db = SQLite.openDatabase({ name: 'test.db', createFromLocation: '~sqlite.db' })
  9.  
  10.  
  11. class Event2 extends Component {
  12.  
  13. constructor(props) {
  14. super(props);
  15.  
  16. this.state = {
  17. header: 'Kit de campo en emergencias En la Salud',
  18. title: 'Enfermedades Infecciosas',
  19. loading: true,
  20. data: []
  21. }
  22.  
  23. }
  24.  
  25. componentDidMount() {
  26.  
  27. this.getRecords(2);//invoco la funcion y le paso como parametro el numero 2
  28. }
  29.  
  30. getRecords(categoria) {
  31. let query = "";
  32. let params = "";
  33. query = "SELECT * FROM categoria"; //query
  34.  
  35. db.transaction((tx) => {
  36. tx.executeSql(query, [], (tx, results) => {
  37. console.log(results);
  38. var len = results.rows.length;
  39. let row = results.rows;
  40. console.log(row);
  41. console.log(row.item(0).nombre);
  42.  
  43. if (len>0) {
  44. this.setState({
  45. data: row, //asignacion de valores
  46. loading:false,
  47. });
  48. console.log(this.state.data.item);
  49. var da = JSON.parse(this.state.data.item);
  50. console.log(da);
  51. this.state.data.item.forEach(element => {
  52. console.log(element);
  53. });
  54. console.log(this.state.data.item.row[0]);
  55. }
  56. else{
  57. this.setState({
  58. loading:false,
  59. });
  60. console.log("No ejecuto la consulta");
  61. }
  62.  
  63.  
  64. }, (error) => {
  65. this.setState({
  66. loading:false,
  67. });
  68. console.log("Error " + JSON.stringify(error))
  69. });
  70. });
  71.  
  72. }
  73.  
  74. keyExtractor = (item, index) => index.toString() //definir el key
  75.  
  76. //renderizar los datos
  77. renderItem = ({ index }) => (
  78.  
  79. <ListItem
  80. title={index.item}
  81. subtitle={index.nombre}
  82. avatar={{ uri: index.imagen }}
  83. />
  84.  
  85. )
  86.  
  87. render() {
  88. if (this.state.loading) {
  89. return (
  90. <View style={styles.container}>
  91. <Text style={styles.welcome}>
  92. Los se estan cargando...
  93. </Text>
  94. </View>);
  95. } else {
  96. return (
  97. <View style={styles.container}>
  98. <View style={styles.header}>
  99.  
  100. </View>
  101. <View style={{ width: "100%", height: '40%' }}>
  102. <View style={{ width: "100%" }}>
  103. <Image
  104. style={{width: '100%', height: '100%'}}
  105. source={require('../images/DiseasesBanner.jpg')}/>
  106. </View>
  107. <View style={{ width: "100%", backgroundColor: 'orange' }}>
  108. <Text style={styles.textCancel}>
  109. {this.state.title}
  110. </Text>
  111. </View>
  112. </View>
  113. <View style={{ width: "75%" }}>
  114. <FlatList
  115. keyExtractor={this.keyExtractor}
  116. data={this.state.data}
  117. renderItem={this.renderItem}
  118. />
  119. </View>
  120. <View style={styles.navInferior}>
  121.  
  122. </View>
  123.  
  124. </View >
  125. );
  126.  
  127. }
  128. }
  129. }
  130.  
  131. item(i) {
  132. return rows[i];
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement