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
推送ListView上的本机选择行时作出反应_Listview_React Native - Fatal编程技术网

推送ListView上的本机选择行时作出反应

推送ListView上的本机选择行时作出反应,listview,react-native,Listview,React Native,当我用十个或更少的元素填充ListView时,我可以在触摸它时选择特定的行并更改行背景色。但是当我在ListView中放入10个以上的元素时,应用程序不会改变this.state.data。如果for循环中的变量大于10,则无法将其状态更改为false/true,并且无法更改行背景颜色 有什么建议吗 class UbicacionScreen extends React.Component { constructor(props) { super(props) var

当我用十个或更少的元素填充ListView时,我可以在触摸它时选择特定的行并更改行背景色。但是当我在ListView中放入10个以上的元素时,应用程序不会改变this.state.data。如果for循环中的变量大于10,则无法将其状态更改为false/true,并且无法更改行背景颜色

有什么建议吗

class UbicacionScreen extends React.Component {

  constructor(props) {
     super(props)

     var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

    this.state = {
      data: [],
      dataSource: ds,
    }

  }

  componentDidMount() {

    var datas = [];

    for (var i = 0; i <= 11; i++) {
       datas.push({
          row: i,
          isSelect: false,
       });
    }

    this.setState({data: datas})
    this.setState({
       dataSource: this.state.dataSource.cloneWithRows(datas)
    })    
  }

  render() {

    return (      
      <ScrollView style={styles.body}>        
        <View style={styles.container}>
          <View style={styles.listReglamentos}>
              <List style={{backgroundColor: '#fff', marginTop: 14}}>
                <ListView
                  renderRow={this._renderRow.bind(this)}
                  dataSource={this.state.dataSource}                  
                />
            </List>
          </View>      
        </View>
      </ScrollView>
    );
  }

  _renderRow(rowData: string, sectionID: number, rowID: number) {
      return (
        <TouchableHighlight onPress={() => { this._onPressRow(rowID, rowData) }}>
          <View style={[rowData.isSelect ? styles.activeRow : styles.deactivedRow, styles.style_row_view]}>
            <Text style={styles.style_text}>{rowData.municipio}</Text>
            <Text style={styles.style_text}>{rowData.isSelect ? 'true' : 'false'}</Text>
          </View>
        </TouchableHighlight>
      );
  }

  _onPressRow = (rowID, rowData) => {
      rowData.isSelect = !rowData.isSelect;
      var dataClone = this.state.data;
      dataClone[rowID] = rowData;
      this.setState({
        data: dataClone
      });
  }

}
class UbicacionScreen扩展了React.Component{
建造师(道具){
超级(道具)
var ds=新的ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
此.state={
数据:[],
数据源:ds,
}
}
componentDidMount(){
var数据=[];
对于(var i=0;i
{rowData.municipio}
{rowData.isSelect?'true':'false'}
);
}
_onPressRow=(rowID,rowData)=>{
rowData.isSelect=!rowData.isSelect;
var dataClone=this.state.data;
dataClone[rowID]=rowData;
这是我的国家({
数据:数据克隆
});
}
}

不需要使用
数据
数组进行比较。您可以将选定的值保存为状态,然后在此行中更改设置

<View style={[
    (rowData.isSelect)
    ? styles.activeRow 
    : styles.deactivedRow
    , styles.style_row_view
]}>



感谢您的回复。我为此修改了代码:我的问题的解决方案是:现在您是否能够获得正确的选择值?是的,主要错误不是指示ListView必须使用initialListSize和数组大小的值进行初始化。谢谢
<View style={[
  (this.state.selectedValue == rowData.municipio) 
    ? styles.activeRow 
    : styles.deactivedRow
    , styles.style_row_view
]}>