Search code examples
pythonkivykivymd

How can I pass an information forward from a dynamically added widget with kivy?


I'm getting data back from an API endpoint which I use to populate a ThreeLineListItem widget with. The problem is that I still couldn't find a way of passing the person['id'] like the example below:

                [...]
                data = response.json() #response from an API request
                if response.status_code == 200:
                    for person in data:
                        print(person['id'])
                        self.ids.person_mdlist.add_widget(
                            ThreeLineListItem(
                                text=str(person['name']),
                                secondary_text=str(person['document']),
                                tertiary_text=str(person['descript']),
                                on_release=lambda x: self.test(x),
                                        ),)

My plan is to grab the selected person ID for future API calls. Is there a simple way of doing that?


Solution

  • I did it! As ThreeLineListItem doesn't have any 'id' property, I had to create a custom widget based on it:

    class CustomThreeLineListItem(ThreeLineListItem):
        list_id = NumericProperty(0)
    

    Then:

    [...]
                    data = response.json() #response from an API request
                    if response.status_code == 200:
                        for person in data:
                            print(person['id'])
                            self.ids.person_mdlist.add_widget(
                                CustomThreeLineListItem(
                                    list_id=person['id'],
                                    text=str(person['name']),
                                    secondary_text=str(person['document']),
                                    tertiary_text=str(person['descript']),
                                    on_release=lambda x: self.test(x),
                                            ),)
    

    Hope it helps someone in the future.