Gatt88
Gatt88

Reputation: 77

Kivy RecycleView. How to refresh the screen after deleting data

What i'm trying to do is find a way to refresh a table made by using recycleview,

If you press one of the label created, my code delete data from table, but not refresh the table on a graphic level

Obviously if you press the same label 2 times, python gives ValueError...

Here's my code: temp.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty

class Sfondo_tabella(RecycleDataViewBehavior, Label):
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos) and self.selectable:
            x = Screen_1()
            x.remove(self.text)

class Screen_1(Screen):
    data_items = ListProperty([])
    data_items = ['1', '2', '3']

    def remove(self, data):
        self.data_items.remove(data)

class TempApp(App):
    pass

if __name__ == '__main__':
    TempApp().run()

temp.kv

ScreenManager:
    id: screen_manager
    name: "screen_manager"

    Screen_1:
        id: screen_1
        name: "Screen_1"
        manager: screen_manager

<Sfondo_tabella>:
    # Draw a background to indicate selection
    color: 0,0,0,1
    font_size:  self.height * 0.5
    text_size: self.width, None
    valign: 'top'
    halign: 'center'

    canvas.before:
        Color:
            rgba: (1, 1, 1, 1)
        Rectangle:
            pos: self.pos
            size: self.size
    canvas:
        Color:
            rgba:0,0,0,1
        Line:
            width:0.5
            rectangle:(self.x,self.y,self.width,self.height)


<Screen_1>:

    BoxLayout:
        RecycleView:
            viewclass: 'Sfondo_tabella'
            data: [{'text': str(x)} for x in root.data_items]
            RecycleGridLayout:
                cols: 10
                size_hint: 1, None
                default_size: None, dp(20)
                default_size_hint: 1, None
                height: self.minimum_height
                width: self.minimum_width

Upvotes: 0

Views: 1161

Answers (1)

amras
amras

Reputation: 1599

In your code you are not updating the RecycleView data. The refresh_view_attrs function should be overloaded to get the index of the label clicked/touched upon and then the data needs to be updated accordingly. Following is the updated code for temp.py:

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty

class Sfondo_tabella(RecycleDataViewBehavior, Label):
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        self.index = index
        return super(Sfondo_tabella, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos) and self.selectable:
            x = Screen_1()
            x.remove(self.text)
            self.parent.parent.data.pop(self.index)

class Screen_1(Screen):
    data_items = ListProperty([])
    data_items = ['1', '2', '3']

    def remove(self, data):
        self.data_items.remove(data)

class TempApp(App):
    pass

if __name__ == '__main__':
    TempApp().run()

This will update properly the data of RecycleView and the GUI accordingly.

Upvotes: 1

Related Questions