Search code examples
androidreact-nativerealm

How to get Data from Realm for my ListView


I have a problem, in my first screen LaunchScreen, i have a button for adding some data in my Realm :

addBdd(realm){
 realm.write(() => {
  realm.create('Bills', {
    type: "Fournitures",
    creditor: "Entreprise A",
    month: "Avril",
    year: "2017",
    price: "40",
  })
});
 alert("Count : " + realm.objects('Bills').length);
}

All is working this far. But after that, i have two views, a LoginScreen, and the View where data must be applied to my list view : ListBillScreen

<ListView
    enableEmptySections={true}
    style={styles.listView}
    dataSource={this.state.dataSource}
    renderRow={(data) => <Row {...data} navigator={this.props.navigator} />}
    renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator} />}
  />

Which calls a class named Row :

function goBillscreen() {
props.navigator.push({ screen: 'BillScreen', props: {
billType : props.type,
billCreditor : props.creditor,
billMonth : props.month,
billYear: props.year,
billPrice: props.price
}});
}

return(
<TouchableHighlight onPress={() => goBillscreen()}>
<View style={styles.container}>
<Image source={{ uri: props.picture}} style={styles.photo} />
<Text style={styles.text}>
  {`${props.type} ${props.creditor} / ${props.month} ${props.year}`}
  {`\n${props.price} euros`}
</Text>
</View>
</TouchableHighlight>
);

The question is : How do I give my realm to the ListBillView to give the data like :

this.state = {
  dataSource: ds.cloneWithRows(datafromrealm)
};

and how can I give type, creditor, month, year and price to Row ?

Sorry for the long post I tried but I'm lost !

Thanks for all !


Solution

  • the question is not well defined but seems you are asking how to process data for every row in your listview. You can check some of the examples we have in realm-js repo like this one

    https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/todo-listview.js#L85

    This is a todo list sample application which uses a ListView to show the todo items.