pythonkivykivy-recycleview

Kivy: refresh RecycleView data


I saw different posts talking about that: HERE and THERE.

But they are complex solutions when I just want to understand what is the exact line that make the changement. I have the access to the RecycleView

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory as F
from kivy.properties import ListProperty

KV = '''
<Item>:
RecycleView:
    data: app.data
    viewclass: 'Row'
    RecycleBoxLayout:
        id: recycle_view
        orientation: 'vertical'
        size_hint: 1, None
        height: self.minimum_height
        default_size_hint: 1, None
        default_size: 0, dp(40)
'''


def _on_text(instance, value):
    recycle_view = instance.app.screen.ids["recycle_view"]
    print(recycle_view,"value that I want to update in the recycle_view:",value)
    #recycle_view.data ? I don't know how to access the data and change it !


class Row(F.TextInput):

    def on_kv_post(self, base_widget):
        super().on_kv_post(base_widget)
        self.app = MDApp.get_running_app()
        self.bind(text=_on_text)

class Application(MDApp):
    data = ListProperty()

    def build(self):
        self.data = [
            {'index': i, 'text': 'hello {}'.format(i)}
            for i in range(40)
        ]
        self.screen = Builder.load_string(KV)
        return self.screen


if __name__ == "__main__":
    Application().run()

Solution

  • Okay, I find out that I missed the fact that I was playing with RecycleBoxlaout and not RecycleView.

    from kivymd.app import MDApp
    from kivy.lang import Builder
    from kivy.factory import Factory as F
    from kivy.properties import ListProperty
    
    KV = '''
    <Item>:
    RecycleView:
        data: app.data
        viewclass: 'Row'
        RecycleBoxLayout:
            id: recycle_boxlayout
            orientation: 'vertical'
            size_hint: 1, None
            height: self.minimum_height
            default_size_hint: 1, None
            default_size: 0, dp(40)
    '''
    
    
    def _on_text(instance, value):
        instance.app.screen.data[instance.index]["text"] = value
    
    
    class Row(F.TextInput):
    
        def on_kv_post(self, base_widget):
            super().on_kv_post(base_widget)
            self.app = MDApp.get_running_app()
            self.counter = -1
            self.bind(text=_on_text)
    
    class Application(MDApp):
        data = ListProperty()
    
        def build(self):
            self.data = [
                {'index': i, 'text': 'hello {}'.format(i)}
                for i in range(40)
            ]
            self.screen = Builder.load_string(KV)
            return self.screen
    
    
    if __name__ == "__main__":
        Application().run()