React native listview不显示所有行

时间:2016-11-26 18:58:46

标签: android listview react-native

我有一个问卷调查应用程序在Android上运行正常。但是在iOS上,ListView从不显示所有问题。调查问卷中的11个问题。只渲染了大约2或2.5个。根据仿真器窗口的大小,似乎会发生少量滚动。我已经尝试了所有发布的建议,包括: - 使用Dimensions设置ListView项的高度 - 设置flex:1向上容器层次结构。 但似乎没有任何效果。 我发布了使用NativeBase的RadionButton的代码的删节和修改版本。我的原始代码使用我自己的自制单选按钮,但所有这些都有同样的问题。我很感激我能解决这个问题的任何帮助。提前致谢。 这是我的代码:

import React, { Component } from 'react';
import {
  Alert,
  StyleSheet,
//  Text,
  TextInput,
  View,
  ListView,
  TouchableHighlight,
  Dimensions,
} from 'react-native';

import Svg,{
    Rect,
} from 'react-native-svg';

import { Actions } from 'react-native-router-flux';
import realm from './realm';
import Utils from './Utils';
import moment from 'moment';

import { Container, Content, InputGroup, Input,
     List, ListItem, Text, Radio } from 'native-base';
require('Dimensions');
const windowDims = Dimensions.get('window');

///////////////////////////////////////////////////////////////////////////////
const WhenSymptom = 0;
const FrequencySymptom = 1;
const FeelingSymptom = 2;
const YesNoSymptom = 3;
const ValueSymptom = 4;

global.junk = "hello";

var SymptomValues = new Array
  (
   // WhenSymptom
   [ "none", "only after inactivity", "mostly AM", "AM and PM", "all the time" ],
   // FrequencySymptom
   [ "None", "Less than usual", "Usual", "More than usual", "Very bothersome" ],
   // FeelingSymptom
   [ "Great", "Fair", "So-so", "Poor", "Terrible" ],
   // YesNoSymptom
   [ "No", "", "", "", "Yes" ],
   // ValueSymptom
   []
   );

// var questions = new Array
global.questions = new Array
  (
   {   rowIndex: 0,
       chartTitle: "Tremors",
       answer: 0},
   {   rowIndex: 1,
       chartTitle: "Speech-slurring",
       answer: 1},
   {   rowIndex: 2,
       chartTitle: "Feeling-stuck",
       answer: 2},
   {   rowIndex: 3,
       chartTitle: "Instability",
       answer: 3},
   {   rowIndex: 4,
       chartTitle: "Anxiety",
       answer: 4},
   {   rowIndex: 5,
       chartTitle: "# of steps",
       answer: 1},
   {   rowIndex: 6,
       chartTitle: "Pain or aches",
       answer: 3},
   {   rowIndex: 7,
       chartTitle: "Day drowsiness",
       answer: 2},
   {   rowIndex: 8,
       chartTitle: "Urinary urgency",
       answer: 1},
   {   rowIndex: 9,
       chartTitle: "Constipation",
       answer: 0},
   {   rowIndex: 10,
       chartTitle: "Overall feeling",
       answer: 2}
   );

var colors = new Array( "green", "#dbdb70", "yellow", "orange", "red" );

var styles = StyleSheet.create({
  list: {
    flexDirection: 'column',
    flex:1,
    //height: windowDims.height-30,
    marginTop: 52,
  },
  questionText: {
    flex:1,
    fontSize: 14,
    // color: 'white',
    color: 'black',
    alignSelf: 'flex-start'
  },
});

var junk = 0;

// const mySaveAction = () => console.log('Back From the Questions page ');

///////////////////////////////////////////////////////////////////////////////
export default class QuestionsPage extends Component {
  constructor(props){
    super(props);
    console.log("numToPop: " + this.props.numToPop);
    var ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 != r2
      //rowHasChanged: (r1, r2) => true
    });
    // if it is a new panel, set all the answers to -1
    let answersIn = new Array(questions.length);
    if (this.props.panel == null) {
      for (let i in questions) {
    answersIn[i] = -1;
    questions[i].answer = -1;
      }
    }
    else {
      answersIn = this.props.panel.symptoms.split(",");
      for (let i in questions) {
    questions[i].answer = answersIn[i];
      }
    }
    this.state = {
      ds: questions,
      dataSource:ds,
      editAllowed: 0,
      initialAnswers: answersIn,
      behavior: 'position' 
      // there is three ways to adjust (position , height , padding ) 
    }

  }

  ///////////////////////////////////////////////////////////////////////////////
  componentDidMount(){
    this.setState({
      dataSource:this.state.dataSource.cloneWithRows(this.state.ds),
    });
    // When we come in here with newPanel == 1, the route was
    // MenuPage => QuestionsPage; so cancel should just be
    // Actions.pop(). If newPanel == 0, , the route was
    // MenuPage => Calendar => QuestionsPage; so cancel should call Actions.pop(2)
    Actions.refresh({ onRight: () => { this._saveButtonPressed(); },
      onBack: () => { this._cancelButtonPressed(); },
      title: (this.props.panel == null ? 
          "New panel" : 
          ("     Panel of " + 
           Utils.readableDate(this.props.panel.panelDate)))
      });
  }

  ///////////////////////////////////////////////////////////////////////////////
  _saveButtonPressed() {
    var finalAnswers = new Array(questions.length);
    var panelChanged = 0;
    var dateString;
    for (let i=0; i<finalAnswers.length; i++) {
      finalAnswers[i] = questions[i].answer;
      if (finalAnswers[i] != this.state.initialAnswers[i])
    panelChanged = 1;
    }
    var symptoms = "" + finalAnswers[0];
    for (let i=1; i<finalAnswers.length; i++) {
      symptoms = (symptoms + "," + finalAnswers[i]);
    }
    console.log("_saveButtonPressed panelChanged: " + panelChanged);
    if (this.props.panel == null) {
      if (panelChanged) {
    let myPanelKey = Utils.newPanelKey();
    this._updateDB(myPanelKey, symptoms);
      }
      Actions.pop();
    }
    else {
      if (panelChanged) {
    this._updateDB(this.props.panel.panelDate, symptoms);
      }
      // Kluge:
      this._returnFromScreen();
    }
  }

  ///////////////////////////////////////////////////////////////////////////////
  _updateDB(myPanelDate, symptoms) {
    console.log("_updateDB dt: " + moment(myPanelDate).toString() +
        " symptoms: " + symptoms);
    // var dateString = Utils._myISOString(myPanelDate);
    realm.write(() => {
    realm.create('Symptoms', {panelDate: myPanelDate, symptoms: symptoms}, true);
      });
    console.log("Trace");
    let after = realm.objects('Symptoms');
    for (let i=0; i<after.length; i++) {
      console.log("panelDate: " + moment(after[i].panelDate).toString() +
          " (" + after[i].panelDate + " )" +
          "; symptoms: " + after[i].symptoms);
    }
  }

  ///////////////////////////////////////////////////////////////////////////////
  _returnFromScreen() {
    if (this.props.numToPop == 1)
      Actions.pop();
    else
      Actions.pop({popNum: 2});
  }

  ///////////////////////////////////////////////////////////////////////////////
  _cancelButtonPressed() {
    console.log("_cancelButtonPressed in QuestionsPage numToPop: " +
        this.props.numToPop);
    this._returnFromScreen();
  }

  ///////////////////////////////////////////////////////////////////////////////
  _rowPressed(rowData){
    console.log("In _rowPressed");
  }

  ///////////////////////////////////////////////////////////////////////////////
  _buttonPressed(rowIndex, value, forceEdit = 0){
    // console.log("In _buttonPressed " + rowIndex + " " + value);
    let newDs = this.state.ds.slice();
    newDs[rowIndex] = {
      ...this.state.ds[rowIndex],
      answer: value,
    };
    // This is absolutely essential to do!!!!!
    questions[rowIndex].answer = value;
    this.setState({
      dataSource:this.state.dataSource.cloneWithRows(newDs),
    })
    // this.forceUpdate();
    // console.log("answers: " + this.state.answers);
  }

  ///////////////////////////////////////////////////////////////////////////////
  _renderButton(rowIndex, column){
      return (
      <ListItem>
          <Radio selected={this.state.ds[rowIndex].answer == column ? true : false} />
          <Text>{SymptomValues[0][column]}</Text>
      </ListItem>
       );
  }

  ///////////////////////////////////////////////////////////////////////////////
  _renderRow(rowData){
    // console.log("renderRow " + this.state.ds[rowData.rowIndex].answer);
      return (
      <View style={styles.list}>
            <Text style={styles.questionText}>Question {rowData.rowIndex+1}.</Text>
            <Container>
              <Content>
              <List>
              { this._renderButton(rowData.rowIndex, 0) }
              { this._renderButton(rowData.rowIndex, 1) }
              { this._renderButton(rowData.rowIndex, 2) }
              { this._renderButton(rowData.rowIndex, 3) }
              { this._renderButton(rowData.rowIndex, 4) }
              </List>
              </Content>
            </Container>
      </View>
      );
  }

  ///////////////////////////////////////////////////////////////////////////////
  render(){
    // console.log("render");
    return (
           <ListView style={styles.list}
              dataSource = {this.state.dataSource}
              renderRow = {this._renderRow.bind(this)}>
           </ListView>
    );
  }

}

module.exports = QuestionsPage;


// Util.js
import moment from 'moment';

let Utils = {
  // toISOString returns (YYYY-MM-DDTHH:mm:ss.sssZ
 newPanelKey: function() {
    let myPanelDate = moment();
    myPanelDate.milliseconds(0);
    myPanelDate.seconds(0);
    myPanelDate.minutes(0);
    return myPanelDate.valueOf();
  },

 readableDate: function(utc) {
    return moment(utc).format("MMM D, YYYY@ha");
  },

 readableDateNoTime: function(utc) {
    return moment(utc).format("MMM D, YYYY");
  },

 shortDate: function(utc) {
    let mom = moment(utc);
    let date = mom.format("DDMMM:hha");
    return date.substring(0,date.length-1);
  }
}

module.exports = Utils;

0 个答案:

没有答案