V
V
Vladislav Bayramov2017-04-14 10:53:06
Android
Vladislav Bayramov, 2017-04-14 10:53:06

How to pass json content to react-native ListView?

Hey! I take json (code below) and display it as a list. When I display in ScrollView - it works. But I need to display it in a ListView and I don’t understand how to pass parameters to it.
json

{
  "items": [
    {
      "id": 1,
      "name": "some1",
      "sort": 1,
      "created_at": 1487066340,
      "updated_at": 1487066340
    },
    {
      "id": 2,
      "name": "some2",
      "sort": 2,
      "created_at": 1487066350,
      "updated_at": 1487066350
    }
  ],
  "_links": {
    "self": {
      "href": "http://....."
    }
  },
  "_meta": {
    "totalCount": 2,
    "pageCount": 1,
    "currentPage": 1,
    "perPage": 10
  }
}

My code
class MainCategoryContainer extends Component {

    constructor(props) {
        super(props);
        const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
        this.state = {
            page: 0,
            dataSource: ds.cloneWithRows(props.items)
        };

        this._next = this._next.bind(this);
        this._onCloseModal   = this._onCloseModal.bind(this);
    }

    componentWillMount() {
        this.props.getList();
        this.props.getListChild();

    };
    renderRow(item) {
        return (
            <View key={items.id}>
                <TouchableHighlight
                    style={[styles.button]}
                    underlayColor={TOUCH_COLOR}
                >
                    <View style={styles.listItem}>
                        <Text style={styles.buttonText}>{items.name}</Text>
                        <EvilIcon style={styles.mainIcon} name='chevron-right' size={48} />
                    </View>
                </TouchableHighlight>
            </View>
        )
    };
    render() {
        const sliderViewProps = {
            ref: "pager",
            initialPage: 0,
            style: styles.sliderContainer,
            indicator: <PagerDotIndicator style={styles.pageIndicator} pageCount={4} dotStyle={styles.indicatorDot}
                                          selectedDotStyle={styles.selectedIndicatorDot}/>
        };

        const {
            getListRequest, getListError, items
        } = this.props;

        if (getListRequest || getListError !== null) {
            return <View style={styles.fullScreen}>
                <ModalWindow isVisible      = {getListRequest || getListError !== null}
                             isLoading      = {getListRequest}
                             isError        = {getListError !== null}
                             errorText      = {getListError}
                             onRequestClose = {this._onCloseModal}
                />
            </View>
        };

        setInterval(() => {this._next()}, 3500);

        const renderList = items.items.map(function (items) {
            return (
                <View key={items.id}>
                    <TouchableHighlight
                        style={[styles.button]}
                        underlayColor={TOUCH_COLOR}
                        >
                        <View style={styles.listItem}>
                            <Text style={styles.buttonText}>{items.name}</Text>
                            <EvilIcon style={styles.mainIcon} name='chevron-right' size={48} />
                        </View>
                    </TouchableHighlight>
                </View>
            )

        });

        return <View>
            <IndicatorViewPager {...sliderViewProps}>
            <View><ImageWrapper
                imageUrl='http://www.sdelka-invest.com/img/hello.jpg'>{null}</ImageWrapper></View>
            <View><ImageWrapper
                imageUrl='https://pokupkamashiny.ru/wp-content/uploads/2016/10/maxresdefault.jpg'>{null}</ImageWrapper></View>
            <View><ImageWrapper
                imageUrl='http://hyundai-club.kiev.ua/wp-content/uploads/2017/01/32000-thumb.jpg'>{null}</ImageWrapper></View>
            <View><ImageWrapper
                imageUrl='https://previews.123rf.com/images/kzenon/kzenon1312/kzenon131200210/24431786-Man-buying-a-car-in-dealership-sitting-in-his-new-auto-they--Stock-Photo.jpg'>{null}</ImageWrapper></View>
        </IndicatorViewPager>
            <View>
                <ListView
                    dataSource={this.state.dataSource}
                    renderRow={items => this.renderRow(items)}
                    enableEmptySections={true}
                />
            </View>
        </View>

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
carroll, 2017-04-15
@carroll

const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }) put in front of the class.

<ListView
  dataSource={ds.cloneWithRows(this.state.dataSource)}
  renderRow={items => this.renderRow(items)}
  enableEmptySections={true}
/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question