Hello All,
SQLite表:
表名 :用户
列 :user_id,名称
我们尝试将User_id和User详细信息的名称显示在FlatList中,但我面临的问题是将用户对象放入数组和数组对象列表以显示为FlatList。
我已经在下面显示了stackoverflow的链接,但我仍面临问题:
import React, { Component } from 'react';
import { Text,
View,
Button,FlatList,ListItem,
TextInput,Alert,
StyleSheet } from 'react-native';
var SQLite = require('react-native-sqlite-storage')
var db = SQLite.openDatabase({name: 'sqliteexample.db', createFromLocation: '~sqliteexample.db'})
const count = 0;
type Props = {};
export default class HelloWorldApp extends Component<> {
constructor(props){
super(props)
this.state = {
user_id:'',
name:'',
record:[],
data:{user_id:'',name:''},
dataSource: [{user_id:'1',name:'a1'},{user_id:'1',name:'a1'},{user_id:'1',name:'a1'}],
};
db.transaction(function (txn) {
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
var len = res.rows.length;
console.log('mylength: ' + len);
for (let i = 0; i < len; i++) {
console.log('mylength: ' + res.rows.item(i).name);
this.setState({dataSource: this.state.dataSource.put(res.rows.item(i).name)});
}
});
});
}
onPressInsert (){
var uid = this.state.user_id;
var uname = this.state.name;
console.log('lengths I ' + uid + ' abc ' + uname);
db.transaction(function (txn) {
txn.executeSql('INSERT INTO Users (name,user_id) VALUES (:name,:user_id)', [uname,uid]);
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
for (let i = 0; i < res.rows.length; ++i) {
let row = res.rows.item(i);
console.log('Select Id a : ' + dataSource.length);
}
Alert.alert('Insert Id: ' + uid + ' Name: ' + uname + ' successfully');
});
});
}
onPressUpdate () {
var uid = this.state.user_id;
var uname = this.state.name;
console.log('lengths U ' + uid + ' abc ' + uname);
db.transaction(function (txn) {
txn.executeSql('UPDATE `Users` SET name=? WHERE user_id=?', [uname,'3']);
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
Alert.alert('Update Id: ' + uid + ' Name: ' + uname + ' successfully');
});
});
}
onPressDelete () {
var uid = this.state.user_id;
var uname = this.state.name;
console.log('lengths D ' + uid + ' abc ');
db.transaction(function (txn) {
txn.executeSql('DELETE FROM `Users` WHERE492 `user_id`=?', [uid]);
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
Alert.alert('Delete Id: ' + uid + ' Name: ' + uname + ' successfully');
});
});
}
onPressSelect () {
db.transaction(function (txn) {
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
for (let i = 0; i < res.rows.length; ++i) {
Alert.alert('Select Id: ' + uid + ' Name: ' + uname + ' successfully');
}
});
});
}
render() {
return (
<View>
<TextInput
style={styles.welcome}
placeholder="Enter Id"
editable = {true}
value = {this.state.user_id}
onChangeText={(text) => this.setState({user_id:text})}
maxLength = {30}
/>
<TextInput
style={styles.welcome}
value = {this.state.name}
placeholder="Enter Full Name"
editable = {true}
onChangeText={(text) => this.setState({name:text})}
maxLength = {30}
/>
<View>
<Button
onPress={this.onPressInsert.bind(this)}
title="Insert"
color="#841584"/>
<Button
onPress={this.onPressUpdate.bind(this)}
title="Update"
color="#841584"/>
<Button
onPress={this.onPressDelete.bind(this)}
title="Delete"
color="#841584"/>
</View>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text> Id is :: {item.user_id} Name is :: {item.name}</Text>}
keyExtractor={(item, index) => index+''}
/>
<FlatList data={this.state.record}
keyExtractor={(x,i) => 1}
renderItem={ ({item}) =>
<ListItem><Text>{item.name}</Text></ListItem>
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
itemBlock: {
flexDirection: 'row',
paddingBottom: 5,
},
itemImage: {
width: 50,
height: 50,
borderRadius: 25,
},
itemMeta: {
marginLeft: 10,
justifyContent: 'center',
},
itemName: {
fontSize: 20,
},
itemLastMessage: {
fontSize: 14,
color: "#111",
}
});
答案 0 :(得分:0)
您的初始dataSource
是一个对象数组,但在从DB
读取时,您只考虑了name
。您需要将name, user_id
放入数组中。您已使用put
向数组添加数据,但put
不是数组方法,您需要将put
替换为push
,请考虑以下代码段
db.transaction(function (txn) {
txn.executeSql('SELECT * FROM Users', [], function (tx, res) {
var len = res.rows.length;
var data = [];
console.log('mylength: ' + len);
for (let i = 0; i < len; i++) {
console.log('mylength: ' + res.rows.item(i).name);
data.push({ res.rows.item(i).name, res.rows.item(i).user_id});
}
this.setState({dataSource: [...this.state.dataSource, ...data]});
});
首先,使用来自数据库的结果填充data
数组,然后将其更新为state
,请注意,这将append
dataSource
内的值。要避免这种情况并仅将新值填充到dataSource
中,您需要将setState
语句更改为
this.setState({dataSource: data});
希望这会有所帮助!