我在sqlite文件中有2个表,我试图根据选择器选择更改列表视图的内容。
查询运行正常,但listview仍显示旧数据。 我如何强制列表视图回收新数据?
如果我使用搜索输入,则会显示新数据。
constructor(props) {
super(props);
this.state = {
query: null,
selected1: 'table01',
progress: [],
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => { row1 !== row2; },
}),
searchText: '',
filteredData: [],
};
}
componentWillMount() {
this.callQuery();
}
componentWillUnmount() {
this.closeDatabase();
}
callQuery() {
if (this.state.selected1 === 'table01') {
this.setState({ query: 'select * from table01' }, this.openDB());
} else if (this.state.selected1 === 'table02') {
this.setState({ query: 'select * from table02' }, this.openDB());
} else {
console.log('Error creating query');
}
}
openDB() {
const that = this;
db.transaction((tx) => {
tx.executeSql(this.state.query, [], (tx, results) => {
console.log('Query completed');
that.setState(that.state);
const len = results.rows.length;
this.setState({ progress: [] });
for (let i = 0; i < len; i++) {
const row = results.rows.item(i);
const obj = {
code: row.cod,
name: row.name,
};
that.state.progress.push(obj);
}
that.setState(that.state);
});
});
}
setSearchText = (event) => {
const searchText = event.nativeEvent.text;
const text = searchText.toLowerCase();
const data = this.state.progress;
const filteredArray = data.filter((item) => {
if (item.name && item.code) {
const itemName = item.name.toLowerCase();
const codeName = item.code.toLowerCase();
return (itemName.search(text) !== -1 || codeName.search(text) !== -1);
}
return false;
});
this.setState({
filteredData: filteredArray,
searchText,
});
};
onValueChange(key: string, value: string) {
const newState = {};
newState[key] = value;
this.setState(newState);
this.callQuery();
}
renderProgressEntry(entry) {
return (<View>
<Text style={styles.backTextWhite}>{entry.name}</Text>
</View>);
}
render() {
const ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => { row1 !== row2; } });
const data = this.state.searchText ? this.state.filteredData : this.state.progress;
return (
<Picker
mode="dropdown"
selectedValue={this.state.selected1}
onValueChange={this.onValueChange.bind(this, 'selected1')}
>
<Item label="table01" value="table01" />
<Item label="table02" value="table02" />
</Picker>
<Input
onSubmitEditing={this.setSearchText.bind(this)}
autoCapitalize="none"
autoCorrect={false}
returnKeyType="go"
/>
<ListView
initialListSize={1}
pageSize={1}
removeClippedSubviews
enableEmptySections
dataSource={ds.cloneWithRows(data)}
renderRow={this.renderProgressEntry}
style={{ flex: 1 }}
/>
答案 0 :(得分:1)
问题是没有正确设置的选择器值,还添加了箭头函数调用。
onValueChange(key: string, value: string) {
this.setState({
selected1: value,
}, () => { this.callQuery(); });
}