如何在React Native中更改ListView中按钮的文本?

问题描述:

点击后,我试图快速更改列表视图中的按钮的文本吗?

I am trying to change text of button which is in listview quickly after click on it?

我正在尝试使用以下代码来实现此目的,但我无法做到这一点,即它无法正常工作.我该怎么做?请帮忙.

I am trying with following code to implement this but i cant do it i.e its not working. How can i do it? please help.

constructor(props) {
    super(props);
    this.state = {
      button_text:"Connect",
    }
  }

  ConPressed() {
    this.setState({
      button_text: "Connected",
    });
  }

  render() {
    return (
      <ListView
        dataSource={this.state.sa}
        renderRow={(rowData) => 

       <TouchableHighlight onPress={() => this.ConPressed()}>
          <Text>{this.state.button_text}</Text>
       </TouchableHighlight>

      />
    );
  }

export default class ListItem extends Component {
  constructor(props) {
    super(props);

    this.state = {
      button_text: 'Connect',
    }
  }


  ConPressed = () => {
    this.setState({ button_text: 'Connected' });
  }


  render() {
    return (
      <TouchableHighlight onPress={this.ConPressed}>
        <Text>{this.state.button_text}</Text>
      </TouchableHighlight>
    );
  }
}

因此,现在您要在原始文件中导入ListItem,并在renderRow中使用它.

So now you want to import your ListItem in your original file, and use that in your renderRow.

renderRow={() => <ListItem />}