Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在React Native中删除ListView项_Listview_React Native_React Native Listview - Fatal编程技术网

在React Native中删除ListView项

在React Native中删除ListView项,listview,react-native,react-native-listview,Listview,React Native,React Native Listview,我很难从列表视图中删除一行。它将删除列表的最后一个元素,无论单击哪个但是,console.log告诉我,数组已正确删除该项,但渲染错误…internet上已有一些解决方案,但没有一个能帮助我解决问题 日志 如您所见,它实际上删除了正确的项,但显示了错误的数组 11月6日22:18:39维维安空中提醒[25992]:{rowID:'0'} 11月6日22:18:39 Viviennes空中提醒[25992]:[“测试2”,“测试3”] 11月6日22:18:39 Viviennes空中提醒[25

我很难从列表视图中删除一行。它将删除列表的最后一个元素,无论单击哪个但是,console.log告诉我,数组已正确删除该项,但渲染错误…internet上已有一些解决方案,但没有一个能帮助我解决问题

日志

如您所见,它实际上删除了正确的项,但显示了错误的数组

11月6日22:18:39维维安空中提醒[25992]:{rowID:'0'}
11月6日22:18:39 Viviennes空中提醒[25992]:[“测试2”,“测试3”]
11月6日22:18:39 Viviennes空中提醒[25992]:{u rowHasChanged:[功能:rowHasChanged],
_getRowData:[函数:defaultGetRowData],
_sectionHeaderHasChanged:[函数],
_getSectionHeaderData:[函数:defaultGetSectionHeaderData],
_dataBlob:{s1:['测试2','测试3']},
_dirtyRows:[[false,false,true]],
_dirtySections:[假],
_缓存计数:3,
行标识:['0','1','2']],
SectionIdentifies:['s1']}

这是我的构造函数:

constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
  items: [],
  dataSource: ds.cloneWithRows([]),
  newItem: "",
};}
列表视图:

<ListView
        style={styles.container}
        dataSource={this.state.dataSource}
        enableEmptySections={true}
        renderRow={(data, sectionID, rowID) => this.renderRow(data, sectionID, rowID)}
        renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator}/>}
      />

我已经花了两个小时试图解决这个问题,我很确定我做的每件事都是正确的。

我同意Burak Karasoy的观点,但代码中还有一个错误

var newList= this.state.items.splice(index, 1)
newList将包含已删除的项目,但实际上我们需要的是删除项目后的新列表

因此,使代码正常工作所需的更改是

不要在状态对象中存储
项。而是将其存储为类实例变量,并设置
数据源
,如下面的代码所示

this.items=this.getItems();
this.setState({
        dataSource: this.state.dataSource.cloneWithRows(this.items)
 });
现在更新您的
\u delete
方法,如下所示

_delete(index) {
    this.items.splice(index, 1)// This will remove the element at index, and update this.items with new array
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(this.items)
    });
}

谢谢解决了我的问题。
this.items=this.getItems();
this.setState({
        dataSource: this.state.dataSource.cloneWithRows(this.items)
 });
_delete(index) {
    this.items.splice(index, 1)// This will remove the element at index, and update this.items with new array
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(this.items)
    });
}