2016-09-12 1 views
0

リストビューが異なる単一のページが必要です。私はあなたがやろうとしているかわからないんだけど、私はあなたの問題の一つは、ということだと思います異なるリストビューのイベントを処理するときにsetStateが更新されない

var width = Dimensions.get('window').width - 20; 
var page = 6; 

class FeedUserGrid extends Component { 
constructor(props) { 
    super(props); 
    var ds = new ListView.DataSource({ 
     rowHasChanged: (r1, r2) => r1 != r2 
    }); 
    this.state = { 
     dataSource:ds, 
     searchString:'', 
     search_result:PropTypes.node, 
     test_item:PropTypes.node, 
     loaded: false,  
    width: (width/3), 
    height:(width/3) , 
    backgroundColor:'#ededed', 
    marginTop:5, 
    marginBottom:5, 
    marginRight:5, 
    marginLeft:5, 
    alignItems:'center', 
    borderRadius:10, 
    borderColor:'black', 
    borderWidth:0.1, 

    }; 

} 

fetchDataPhoto() { 

    fetch('https://Example.com/..........') 
     .then((response) => response.json()) 
     .then((responseData) => { 
     this.setState({ 
      search_result:responseData, 
      'loaded': true 

     }); 

     this.setState({ 
      dataSource:this.state.dataSource.cloneWithRows(this.state.search_result), 
     }); 
     }) 
     .done(); 

    } 


    renderRow(rowData){ 
    return (
     <View style={{ 
    width: this.state.width, 
    height: this.state.height, 

     }}> 
       <View style={ styles.s }> 

       <Text style={styles.price}>userId:{rowData.userId}</Text> 
       <Text style={styles.Itemtitle}>id:{rowData.id}</Text> 
       <Text style={[styles.Itemtitle]}>title:{rowData.title}</Text> 
       </View> 

      </View> 
    ) 
    } 


    renderScene(route, nav) { 



<ScrollView ref="scrollView" style={{flex: 1, position:'absolute',top:50,left:0,right:0,bottom:50}} > 
       { 
        this.state.loaded && 


       <ListView   
      contentContainerStyle={{   
      flex: 1, 
      flexDirection: 'row', 
      flexWrap: 'wrap', 
      justifyContent: 'center', 
      alignItems:'center' 
        }} 
       dataSource={this.state.dataSource} 
       renderRow={this.renderRow.bind(this)}/> 
       } 
       </ScrollView> 

      // handling event ChangeItemSize, I need different view I writen below. 

     <TouchableOpacity onPress={this.changeItemSize.bind(this)}> 
     <View style={[styles.selectedItemView]}> 
     <Icon name="cutlery" style={[styles.iconItemSetting,styles.navBarBottomIcon,styles.selectedItem]} /> 
     </ View> 
     </ TouchableOpacity> 
    } 


    // different view for updating setState Style changing hight and width 


changeItemSize(){ 
     page+=1 
     if(page == 11){ 
     page = 6; 
     } 

    switch (page) { 
      case 6: 
      this.setState({ 
     width: width/3 - 20, 
     height: width/3 - 20, 
      }); 
      break; 
      case 7: 

      this.setState({ 
     width: width/1, 
     height: width/4 - 20, 

     }); 
      break; 
      case 8: 

      this.setState({ 
      width: width/1, 
     height: width/3, 

    }); 
      break; 
      case 9: 

      this.setState({ 
     width: width/1, 
     height: width/2, 

      }); 
      break; 
      case 10: 

      this.setState({ 
     width: width/1, 
     height: width/1.3, 
      }); 
      break; 
      default: 

      this.setState({ 
     width: width/3 - 20, 
     height: width/3 - 20, 
      }); 
      break; 
     } 


    } 
+0

状態のキーは 'String'であってはなりません。 'Object'でなければなりません。 'loaded ':' loaded:true'のように引用符を削除する必要があります。 –

答えて

0

グリッドのような別のビューを変更するために更新されていないSETSTATEでスタイリングを使用し、大、中、...データが状態に設定されていませんか?

このようfetchDataPhoto方法を更新してみてください。

fetchDataPhoto() { 
    fetch('https://Example.com/..........') 
    .then((response) => response.json()) 
    .then((responseData) => { 
     this.setState({ 
     search_result: responseData, 
     dataSource: this.state.dataSource.cloneWithRows(responseData), 
     loaded: true 
     }); 
    }) 
    .done(); 

} 

問題は非同期にそのsetStateが実行され、そしてあなたがsearch_resultを取得しようとする時点で、それは、まだ状態で存在していないので、あなたが取得していますa null値。

また、dataSourceを上書きしているため、データが読み込まれたときに上書きしないように別の名前を定義する必要があります。

あなたのコードで改善できることは他にもたくさんありますが、私はあなたがアイテムのリストを見ることができるように提案しています。

幸運を祈る!

+0

私を案内してくれてありがとう。私は反応するために新しいです、はい私はもっと多くを改善しなければなりません、私はこれを更新しました。しかし、私の問題はそれです。 listview itemグリッドビュー、巨大ビュー、ラージアイテムビューなどのさまざまなビューを表示する必要があります。リストビューアイテムの高さと幅を変更して、setState値の状態を更新するときに異なるビューにset状態を使用しました。更新中ではありません。私が間違っていたところ、私は得られなかった。あなたの時間のおかげで@クリスフェル。 –

0

私は同じ問題を抱えていた、と私は、このように状態を設定することによってそれを解決:

this.state = { 
    ...this.state, 
    search_result:responseData, 
    'loaded': true 
} 

は基本的に、私は新しいオブジェクトに状態を設定していて、それは大丈夫だと完全に動作します。

+0

リストビューアイテムのスタイルを変更する私はステートをステートで記述しましたが、私は別のリストビューアイテムスタイルの別のボタンのonpressイベントを使用していますが、ステートを変更するとします。状態値は変化していますが、リストビュー項目では更新されません。 this.state = { 幅(幅/ 3)、 高さ:(幅/ 3)、 のbackgroundColor: '#はededed' marginTop:5、 marginBottom:5、 marginRight:5、 marginLeft :5、 alignItems: 'center'、 borderRadius:10、 borderColor: 'black'、 borderWidth:0.1、 }; –

+0

状態にスタイルを追加しません。それは良くない。状態に基づいてスタイルを変更します。たとえば: 'let decideStyle = this.state.loaded? styles.style1:styles.style2'そして次にボタンの中に 'style = {ss}'のようなスタイルを追加してください –

+0

初めての書き込みで、動作していますが、状態スタイルを変更した後に更新されません。 ** renderRow rowData){ let itemStyleRow = this.state.loadedItemStyle> 7? styles.item6:styles.item7; リターン( <ビュースタイル= {[itemStyleRow]}> ) } ** 15リストビュースタイル次いで –

関連する問題